当 DependencyProperites 更改时调用 DependencyObject 的绑定

发布于 2024-08-28 08:22:46 字数 1555 浏览 5 评论 0原文

当内部 DependencyProperties 发生更改时,有没有办法通知 DependencyObject 的绑定?

例如,我有这样的类:

public class BackgroundDef : DependencyObject
    {
        public static readonly DependencyProperty Color1Property =
            DependencyProperty.Register("Color1", typeof(Color),
                typeof(BackgroundDef), new UIPropertyMetadata(Colors.White));

        public static readonly DependencyProperty UseBothColorsProperty =
            DependencyProperty.Register("UseBothColors", typeof(bool),
                typeof(BackgroundDef), new UIPropertyMetadata(false));

        public static readonly DependencyProperty Color2Property =
            DependencyProperty.Register("Color2", typeof(Color),
                typeof(BackgroundDef), new UIPropertyMetadata(Colors.White));

        public Color Color1
        {
            set { SetValue(Color1Property, value); }
            get { return (Color)GetValue(Color1Property); }
        }

        public bool UseBothColors
        {
            set { SetValue(UseBothColorsProperty, value); }
            get { return (bool)GetValue(UseBothColorsProperty); }
        }

        public Color Color2
        {
            set { SetValue(Color2Property, value); }
            get { return (Color)GetValue(Color2Property); }
        }
    }

对于该类,我有 3 个独立的双向绑定,用于设置 Color1、Color2 和 UseBothColors 的值。但我还有一个BackgroundDef 实例的绑定,它应该创建一个Brush 并绘制按钮的背景(单色或两种渐变颜色)。我的问题是 DependencyProperties 的双向绑定更新了属性,但未调用类实例的绑定,因为显然整个对象没有更改。知道当 DependencyProperties 更改时如何调用 DependencyObject 的绑定吗?

Is there a way to notify a DependencyObject's bindinigs when the inner DependencyProperties have changed?

For example, I have this class:

public class BackgroundDef : DependencyObject
    {
        public static readonly DependencyProperty Color1Property =
            DependencyProperty.Register("Color1", typeof(Color),
                typeof(BackgroundDef), new UIPropertyMetadata(Colors.White));

        public static readonly DependencyProperty UseBothColorsProperty =
            DependencyProperty.Register("UseBothColors", typeof(bool),
                typeof(BackgroundDef), new UIPropertyMetadata(false));

        public static readonly DependencyProperty Color2Property =
            DependencyProperty.Register("Color2", typeof(Color),
                typeof(BackgroundDef), new UIPropertyMetadata(Colors.White));

        public Color Color1
        {
            set { SetValue(Color1Property, value); }
            get { return (Color)GetValue(Color1Property); }
        }

        public bool UseBothColors
        {
            set { SetValue(UseBothColorsProperty, value); }
            get { return (bool)GetValue(UseBothColorsProperty); }
        }

        public Color Color2
        {
            set { SetValue(Color2Property, value); }
            get { return (Color)GetValue(Color2Property); }
        }
    }

For which I have 3 separate two-way bindings that set the values for Color1, Color2 and UseBothColors. But I also have a binding for a BackgroundDef instance, which should create a Brush and draw the background of a button (either a single color, or two gradient colors). My problem is that the two-way bindings for the DependencyProperties update the properties, but the binding for the class instance is not called, as apparently the entire object does not change. Any idea how I could call the bindings for the DependencyObject when the DependencyProperties are changed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白色秋天 2024-09-04 08:22:46

您可以:

使用多重绑定并绑定到所有三个值,而不是绑定到类。那么每当其中一个值发生变化时,绑定就会重新评估。 (这是我将使用的技术)

或者:

如果您的类 BackgroundDef 是另一个类的属性,则每当 BackgroundDef' 中的任何一个时,您都可以在该类上引发 NotifyPropertyChanged 事件s 属性发生变化。当然,这意味着在作为其父类的 BackgroundDef 上拥有一个属性,并在子级发生更改时通知父级。

You could:

Use a multibinding and bind to all three values instead of binding to the class. then whenever one of the values changes the binding will re-evaluate. (this is the technique i would use)

Or:

If your class BackgroundDef is a property of another class, you could raise a NotifyPropertyChanged event on that class whenever any of BackgroundDef's properties change. Of course this would mean having a property on BackgroundDefthat is its parents class and notifying the parent whenever the child had changed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文