[英]WPF Data Binding to a string property
I have a question about data binding which I am struggling with. 我有一个关于数据绑定的问题,我正在努力解决。
I have the following property in my xaml.cs file: 我的xaml.cs文件中具有以下属性:
private string _stationIdInstruction;
public event PropertyChangedEventHandler PropertyChanged;
public string StationIdInstruction
{
get { return _stationIdInstruction; }
set
{
_stationIdInstruction = value;
OnPropertyChanged("StationIdInstruction");
}
}
protected void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
How can I bind a TextBlock to StationIdInstructions so it picks up the string property as its Text and update the TextBlock.Text when I update StationIdInstructions. 我如何将TextBlock绑定到StationIdInstructions,以便在我更新StationIdInstructions时将字符串属性用作其Text并更新TextBlock.Text。
Any help is appreciated. 任何帮助表示赞赏。
Yes, and don't forget to specify the binding context. 是的,不要忘记指定绑定上下文。 Eg, 例如,
<Window ... Name="MyWindow">
<Grid DataContext="{Binding ElementName=MyWindow, Path=.}">
<TextBlock Text="{Binding Path=StationIdInstruction}" />
在控件的DataContext上设置StationIdInstructions对象,并像这样设置TextBlock:
<TextBlock Text="{Binding StationIdInstruction}" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.