c# – WPF Xaml绑定不起作用

c# – WPF Xaml绑定不起作用,第1张

概述好吧,我有一个 WPF项目,其中我有4个TexBlock.我想要的是通过绑定更改每个TextBlock的文本. 到目前为止,我有我的XAML: <Grid> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="*"/> <RowDefinition 好吧,我有一个 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-behindDataContext = 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));    }}
总结

以上是内存溢出为你收集整理的c# – WPF Xaml绑定不起作用全部内容,希望文章能够帮你解决c# – WPF Xaml绑定不起作用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1224916.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存