当 DependencyProperites 更改时调用 DependencyObject 的绑定
当内部 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以:
使用多重绑定并绑定到所有三个值,而不是绑定到类。那么每当其中一个值发生变化时,绑定就会重新评估。 (这是我将使用的技术)
或者:
如果您的类
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 ofBackgroundDef
's properties change. Of course this would mean having a property onBackgroundDef
that is its parents class and notifying the parent whenever the child had changed.