[英]WPF Data Binding to a string property
我有一个关于数据绑定的问题,我正在努力解决。
我的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));
}
}
我如何将TextBlock绑定到StationIdInstructions,以便在我更新StationIdInstructions时将字符串属性用作其Text并更新TextBlock.Text。
任何帮助表示赞赏。
是的,不要忘记指定绑定上下文。 例如,
<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.