为什么OneWayToSource绑定会重置我的目标值? 这是绑定代码:
Why is the OneWayToSource binding resetting my target value? Here is the binding code:
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"属性指定起始值,因为它会被绑定覆盖.
I set the "Color" dependency property in xaml. But it gets overwritten by the binding. After that the binding works ok. So, essentially my problem is: I can't give a starting value to the "Color" property because it gets overwritten by the binding.
我做了一个解决该问题的方法,但是仍然不明白为什么OneWayToSource会这样:
I made a workaround that solves the problem, but still don't understand why OneWayToSource behaves like that:
System.Windows.Media.Color CurrentColor = this.Color; this.SetBinding(ColorPicker.ColorProperty, binding); this.Color = CurrentColor;找到了可能的解决方案: 我必须设置:
Found a possible solution: I have to set:
binding.FallbackValue = this.Color;推荐答案
您可以使用 BindingOperations 类来设置绑定:
You could use the BindingOperations class to set the binding:
BindingOperations.SetBinding( brush, SolidColorBrush.ColorProperty, new Binding("Color") { Source = this });更多推荐
OneWayToSource绑定重置目标值
发布评论