3
为什么OneWayToSource绑定重置我的目标值? 这里是绑定代码:OneWayToSource绑定重置目标值
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。但它被绑定覆盖。之后,绑定工作正常。 所以,基本上我的问题是:我不能给“颜色”属性一个初始值,因为它被绑定覆盖。
编辑:
我做了解决该问题的解决办法,但还是不明白为什么OneWayToSource的行为就像是:
System.Windows.Media.Color CurrentColor = this.Color;
this.SetBinding(ColorPicker.ColorProperty, binding);
this.Color = CurrentColor;
编辑2:
发现了一个可能的解决方案: 我必须设置:
binding.FallbackValue = this.Color;
难道你不能只是用另一种方式绑定?像'brush.SetBinding(SolidColorBrush.ColorProperty,新的绑定(“颜色”){源=此});' – Clemens
@Clemens:SolidColorBrush没有SetBinding,这就是为什么我必须去与OneWayToSource。 –
您可能还想阅读[此问题](http://stackoverflow.com/q/4875751/1136211)。 – Clemens