OneWayToSource绑定重置目标值

编程入门 行业动态 更新时间:2024-10-24 12:31:08
本文介绍了OneWayToSource绑定重置目标值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

为什么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绑定重置目标值

本文发布于:2023-11-30 11:57:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649866.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:目标值   绑定   OneWayToSource

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!