我想将字符串值绑定到文本框,但仅在选中复选框时.因此,如果选中该复选框,我希望文本框显示消息1,如果没有,则显示消息2.
做这个的最好方式是什么?是否更好在我的对象中使用List属性,然后取决于是否选中复选框取决于我的List <>中的哪个项目显示
要么
在选中复选框后再更新对象的属性(这次是字符串类型)然后重新绑定会更好吗?
Ber*_*ryl 17
这是一种MVVM类型的方法,假设您了解INotifyPropertyChanged(您需要!).玩它并随时询问你遇到的任何问题.
public class MyViewModel : INotifyPropertyChanged {
const string Msg1 = "blah 1";
const string Msg2 = "blah 2";
private bool _isSelected;
public bool IsSelected{
get { return _isSelected; }
set {
if(_isSelected == value) return;
_isSelected = value;
MyBoundMessage = _isSelected ? Msg1 : Msg2;
NotifyPropertyChanged(()=> IsSelected);
NotifyPropertyChanged(()=> MyBoundMessage);
}
}
public string MyBoundMessage {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
V(查看XAML)
<CheckBox IsChecked="{Binding IsSelected}" />
<TextBox Text="{Binding MyBoundMessage}" />
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
21956 次 |
最近记录: |