一个视图包含几个按钮。在 ViewModel 中,“IsShiftLock”属性驻留在动态创建的绑定中。
public bool IsShiftLock {
get { return _isShiftLock; }
set {
if (value != _isShiftLock) {
_isShiftLock = value;
Notify("IsShiftLock");
}
}
}
Notify 是 BaseViewModel 中的一个方法。
public abstract class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void Notify(string sPropertyName)
{
PropertyChangedEventHandler changedEventHandler = this.PropertyChanged;
if (changedEventHandler == null)
return;
changedEventHandler((object) this, new PropertyChangedEventArgs(sPropertyName));
}
}
Binding b2 = new Binding {
Source = this,
Path = new PropertyPath("IsShiftLock"),
Converter = new ShiftLockToTextConverter()
};
b2.Mode = BindingMode.OneWay;
curKeyView.Button.SetBinding(ContentControl.ContentProperty, b2);
IsShiftLock 正确更改,但转换器调用仅发生一次。据我了解,绑定应适当通知更改。如何做到这一点?
更新1:
视图面:
private readonly KeyboardViewModel viewModel;
public static KeyboardViewModel ViewModelInstance;
public VirtualKeyboard() {
Loaded += OnLoaded;
InitializeComponent();
viewModel = new KeyboardViewModel();
DataContext = viewModel;
ViewModelInstance = viewModel;
}