将.xaml文件中的标签与.cs文件中的string对象绑定可以通过以下步骤实现:
private string _textValue;
public string TextValue
{
get { return _textValue; }
set
{
if (_textValue != value)
{
_textValue = value;
OnPropertyChanged("TextValue");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
<Window.DataContext>
<local:YourViewModel />
</Window.DataContext>
其中,"YourViewModel"是.cs文件的类名。
通过以上步骤,就可以将.xaml文件中的标签与.cs文件中的string对象成功绑定起来。当.cs文件中的属性值发生变化时,标签的Text属性也会相应更新。
领取专属 10元无门槛券
手把手带您无忧上云