我需要一种方法在运行时根据某些值将绑定模式从OneWay更改为TwoWay(主要是如果项目是Visible我将使它成为TwoWay,否则OneWay,有点主/从控件)。
这可能来自xaml吗?
提前致谢。
答案 0 :(得分:1)
您可以通过以编程方式创建绑定,并在条件更改时重新创建绑定。
答案 1 :(得分:0)
我重新绑定了以下内容:
private void OnTextBoxGotFocus(object sender, RoutedEventArgs e)
{
// don't allow to update textbox from source if it has focus
ChangeBindingMode(sender as TextBox, BindingMode.OneWayToSource);
}
private void OnTextBoxLostFocus(object sender, RoutedEventArgs e)
{
ChangeBindingMode(sender as TextBox, BindingMode.TwoWay);
}
private void ChangeBindingMode(TextBox textbox, BindingMode mode)
{
if (textbox == null)
return;
Binding oldBinding = BindingOperations.GetBinding(textbox, TextBox.TextProperty);
// It's not posible to change existing binding, but available to recreate it
// oldBinding.Mode = mode;
BindingOperations.ClearBinding(textbox, TextBox.TextProperty);
Binding newBinding = new Binding(oldBinding.Path.Path);
newBinding.Mode = mode;
BindingOperations.SetBinding(textbox, TextBox.TextProperty, newBinding);
}