c# – WPF Xaml绑定不起作用
发布时间:2020-12-15 19:46:39 所属栏目:百科 来源:网络整理
导读:好吧,我有一个 WPF项目,其中我有4个TexBlock.我想要的是通过绑定更改每个TextBlock的文本. 到目前为止,我有我的XAML: Grid Grid.RowDefinitions RowDefinition Height="*"/ RowDefinition Height="*"/ RowDefinition Height="*"/ RowDefinition Height="*"/
好吧,我有一个
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阶段之后,控件将“绑定”到属性(为空).之后设置属性时,控件无法知道它已更改.有两种机制可以实现这一点.在控件级别,您可以将这些属性设置为 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)); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |