为什么 OneWayToSource 绑定(bind)会重置我的目标值? 这是绑定(bind)代码:
SolidColorBrush brush = GetTemplateChild("PART_PreviewBrush") as SolidColorBrush;
if (brush != null)
{
Binding binding = new Binding("Color");
binding.Source = brush;
binding.Mode = BindingMode.OneWayToSource;
this.SetBinding(ColorPicker.ColorProperty, binding);
}
我在 xaml 中设置了“Color”依赖属性。但是它会被绑定(bind)覆盖。之后绑定(bind)工作正常。 所以,基本上我的问题是:我无法为“Color”属性提供起始值,因为它会被绑定(bind)覆盖。
编辑:
我做了一个解决问题的变通方法,但仍然不明白为什么 OneWayToSource 会这样:
System.Windows.Media.Color CurrentColor = this.Color;
this.SetBinding(ColorPicker.ColorProperty, binding);
this.Color = CurrentColor;
编辑 2:
找到一个可能的解决方案: 我必须设置:
binding.FallbackValue = this.Color;
最佳答案
您可以使用 BindingOperations设置绑定(bind)的类:
BindingOperations.SetBinding(
brush, SolidColorBrush.ColorProperty, new Binding("Color") { Source = this });
关于c# - OneWayToSource 绑定(bind)重置目标值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13772749/