没有Path的Binding

本文介绍在WPF开发中XAML Binding使用.作为Path的场景,演示了当Binding源本身就是数据时如何省略Path,并通过XAML与C#代码示例展示了不同写法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 当Binding源本身就是数据且不需要Path来指明时,可以设置Path的值为".",或直接省略Path。XAML中这个"."可以省略不写,但在C#代码中是不能省略的。

 XAML:

<Window x:Class="没有Path的Binding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="155.129" Width="209.615" Loaded="Window_Loaded">
    <Grid>
        <StackPanel>
            <StackPanel.Resources>
                <sys:String x:Key="myString">
                    菩提本无树,明镜亦非台。
                    本来无一物,何来惹尘埃。
                </sys:String>
            </StackPanel.Resources>
            <TextBlock x:Name="textBlock1" TextWrapping="Wrap"
                       Text="{Binding Path=.,Source={StaticResource ResourceKey=myString}}" FontSize="16" Margin="5" Background="Yellow"></TextBlock>
            <!--标准的写法是上面那个,下面这两个也是一样的-->
            <!--<TextBlock x:Name="textBlock1" TextWrapping="Wrap"
                       Text="{Binding .,Source={StaticResource ResourceKey=myString}}" FontSize="16" Margin="5" Background="Yellow"></TextBlock>-->
            <!--<TextBlock x:Name="textBlock1" TextWrapping="Wrap"
                       Text="{Binding Source={StaticResource ResourceKey=myString}}" FontSize="16" Margin="5" Background="Yellow"></TextBlock>-->
            <TextBlock x:Name="textBlock2" FontSize="16" Margin="5" TextWrapping="Wrap" Background="Green"></TextBlock>
        </StackPanel>
    </Grid>
</Window>

 

 

C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace 没有Path的Binding
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            string str = "菩提本无树,明镜亦非台。本来无一物,何来惹尘埃。";
            this.textBlock2.SetBinding(TextBlock.TextProperty, new Binding(".") { Source = str });
        }
    }
}

 

截图:

 

 

 

 

转载于:https://www.cnblogs.com/KeenLeung/p/3533014.html

如果在WPF中使用数据绑定时,修改了数据但UI没有刷新,可能是由于以下几个原因: 1. 数据源属性没有实现通知属性更改机制:在ViewModel中,需要实现INotifyPropertyChanged接口,并在属性值发生改变时触发PropertyChanged事件。确保在设置属性值后调用OnPropertyChanged方法。 ```csharp private string _myProperty; public string MyProperty { get { return _myProperty; } set { _myProperty = value; OnPropertyChanged(nameof(MyProperty)); } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } ``` 2. 数据绑定未正确指定路径:确保在XAML中,将绑定的Path属性设置为正确的属性路径。例如,如果要绑定到ViewModel中的MyProperty属性,确保路径设置正确。 ```xml <TextBlock Text="{Binding MyProperty}" /> ``` 3. 数据上下文未正确设置:确保将UI元素的数据上下文设置为ViewModel的实例。这可以通过设置Window或UserControl的DataContext属性来实现。 ```xml <Window.DataContext> <local:MyViewModel /> </Window.DataContext> ``` 4. 数据源未正确初始化或更改:在ViewModel中,确保在构造函数或其他适当的位置初始化或更改绑定的属性。这样可以确保初始值或更改后的值被正确地传递到UI。 ```csharp public MyViewModel() { MyProperty = "Initial Value"; } public void UpdateData() { MyProperty = "New Value"; OnPropertyChanged(nameof(MyProperty)); } ``` 如果以上步骤都正确,并且问题仍然存在,可能是由于其他因素导致,比如数据绑定的Mode设置不正确,或者绑定的数据源发生了异常。可以使用调试工具来检查绑定是否正常工作,例如使用Snoop工具来查看绑定的源和目标对象。 希望这些解决方法能帮助到你!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值