微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – WPF Xaml绑定不起作用

好吧,我有一个 WPF项目,其中我有4个TexBlock.我想要的是通过绑定更改每个TextBlock的文本.

到目前为止,我有我的XAML:

<Grid>
    <Grid.RowDeFinitions>
        <RowDeFinition Height="*"/>
        <RowDeFinition Height="*"/>
        <RowDeFinition Height="*"/>
        <RowDeFinition Height="*"/>
    </Grid.RowDeFinitions>
    <TextBlock x:Name="First" Text="{Binding FirstString}" Grid.Row="0"/>
    <TextBlock x:Name="Second" Text="{Binding SecondString}" Grid.Row="1"/>
    <TextBlock x:Name="Third" Text="{Binding ThirdString}" Grid.Row="2"/>
    <TextBlock x:Name="Fourth" Text="{Binding FourthString}" Grid.Row="3"/>
</Grid>

在我的代码我有

public partial class MainWindow : Window
{
    public string FirstString { get; set; }
    public string SecondString { get; set; }
    public string ThirdString { get; set; }
    public string FourthString { get; set; }

    public MainWindow()
    {
        InitializeComponent();    

        FirstString = "First";
        SecondString = "Second";
        ThirdString= "Third
        FourthString= "Fourth";
    }
}

但是Binding根本不起作用.我做错了吗?请帮忙.
提前致谢.

EDIT:

按照Chris Mantle的建议查看debbuger(我已将其设置为Warning sor the Binding)后,我收到以下错误

System.Windows.Data information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=FirstString; DataItem=null; target element is 'TextBlock' (Name='First'); target property is 'Text' (type 'String')

解决方法

有一些事情是不正确的.绑定标记将查看控件的DataContext属性中的对象.除非另有说明,否则此属性从声明父级继承DataContext.开箱即用,对于Window控件,这是null.

这个问题有两种选择.您可以在代码隐藏或XAML中明确设置DataContext

// In XAML
<Window DataContext={Binding RelativeSource={RelativeSource Self}}>

or

// In the code-behind
DataContext = this;

一个问题是在初始化时应用绑定.最初,您的属性为空.在InitializeComponent阶段之后,控件将“绑定”到属性(为空).之后设置属性时,控件无法知道它已更改.有两种机制可以实现这一点.在控件级别,您可以将这些属性设置为DependencyProperty或实现INotifyPropertyChanged接口并引发更改.如果你想进入INPC路线,你可以实现你的属性和Window:

public partial class MainWindow : INotifyPropertyChanged
{
    private string firstString;
    private string secondString;
    private string thirdString;
    private string fourthString;

    public string FirstString
    {
        get { return firstString; }
        set
        {
            firstString = value;
            RaisePropertyChanged("FirstString");
        }
    }

    public string SecondString
    {
        get { return secondString; }
        set
        {
            secondString = value;
            RaisePropertyChanged("SecondString");
        }
    }

    public string ThirdString
    {
        get { return thirdString; }
        set
        {
            thirdString = value;
            RaisePropertyChanged("ThirdString");
        }
    }

    public string FourthString
    {
        get { return fourthString; }
        set
        {
            fourthString = value;
            RaisePropertyChanged("FourthString");
        }
    }

    public MainWindow()
    {
        DataContext = this;
        InitializeComponent();

        FirstString = "First";
        SecondString = "Second";
        ThirdString = "Third";
        FourthString = "Fourth";
    }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    private void RaisePropertyChanged(string propertyName)
    {
        var handlers = PropertyChanged;

        handlers(this,new PropertyChangedEventArgs(propertyName));
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐