从 Shape 派生的类中绑定 Fill 属性
我很难在从 Shape 派生的类中绑定 FillProperty 。
public static readonly DependencyProperty NumberNodeProperty = DependencyProperty.Register("Number", typeof(int), typeof(MyDerivedShape), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsParentMeasure));
public MyDerivedShape( DerivedViewModel viewmModel):Shape
{
DataContext = viewmModel;
Binding FillColorBinding = new Binding("FillColor");
SetBinding(FillProperty, FillColorBinding);
Binding numberBinding = new Binding("Number");
SetBinding(NumberNodeProperty, numberBinding);
}
“FillColor”属性在 DerivedViewModel 继承的基本 Viewmodel 中声明。
“Number”属性在 DerivedViewModel 中声明
FillProperty 是 Shape 基类中的默认依赖属性。
NumberNodeProperty 是在 MyDerivedShape 中声明的依赖属性,
因此,当我更改 DerivedViewModel 中的“Number”时,更改会传播到 Shape (Shape 绘制一个数字)
但是当我更改 DerivedViewModel 中的 FillColor 时,更改不会传播,并且颜色没有改变。我使用的FillColor 是SolidColorBrush 类型。
看来绑定不起作用...这是“FillProperty”依赖属性的“Inherits”属性设置为 false 的结果吗?
I struggle with binding FillProperty in a classe derived from Shape.
public static readonly DependencyProperty NumberNodeProperty = DependencyProperty.Register("Number", typeof(int), typeof(MyDerivedShape), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsParentMeasure));
public MyDerivedShape( DerivedViewModel viewmModel):Shape
{
DataContext = viewmModel;
Binding FillColorBinding = new Binding("FillColor");
SetBinding(FillProperty, FillColorBinding);
Binding numberBinding = new Binding("Number");
SetBinding(NumberNodeProperty, numberBinding);
}
"FillColor" property is declared in a base Viewmodel, from which DerivedViewModel inherits.
"Number" property is declared in DerivedViewModel
FillProperty is the default dependency property in Shape base class.
NumberNodeProperty is a dependency property declared in MyDerivedShape
So , what happens is that when I change "Number" in the DerivedViewModel, change is propagated to the Shape (the Shape draws a number)
But when I change FillColor in the DerivedViewModel, Change is not propagated, and color is not changed . I use FillColor is of type SolidColorBrush.
It seems that the binding does not work... Is it a consequence "Inherits" property set to false for "FillProperty" depedency property ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我回答自己,因为我找到了答案:
事实上,在应用程序的另一段代码中,我做了这样的事情:
这会产生巨大的后果,因为它破坏了我放置的绑定!
它与“依赖属性值优先级”有关,这是我根本不知道的主题。
因此,如果将 ViewModel 属性绑定到依赖属性,则永远不应该直接设置依赖属性。如果您这样做,您的绑定就会丢失!
I answer myself, because I found the answer :
In fact , in another piece of code in the application , I do something like this :
This has huge consequences,as It destroys the Binding I put in place !
It is connected to "Dependency Property Value Precedence", a topic I was not aware at all.
So, if you bind a ViewModel Property to Dependency Property, you should never then set the Dependency Property directly. If you do so, your binding is lost !