c# - OneWayToSource 绑定(bind)重置目标值

标签 c# wpf binding

为什么 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/

相关文章:

c# - WPF 工具栏删除放置在其中的控件的自定义样式

c# - 如何清除列表框中的所有数据?

c# - 在 xamarin ios 中预加载数据库

wpf - 来自另一个线程的 GUI 更新 - WPF Powershell

python - VTK Marching 立方体实现的替代方案

c# - 这是占一定小数位数的有效 float 比较吗?

WPF负载控制问题

wpf - 调用线程无法访问此对象,因为另一个线程拥有它 wpf

在win32(xp、vista等)中将Lua绑定(bind)到Ada?

java - Spring MVC/@InitBinder 没有效果