从 Shape 派生的类中绑定 Fill 属性

发布于 2024-12-05 10:15:22 字数 1105 浏览 1 评论 0原文

我很难在从 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 技术交流群。

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

发布评论

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

评论(1

失与倦" 2024-12-12 10:15:22

我回答自己,因为我找到了答案:

事实上,在应用程序的另一段代码中,我做了这样的事情:

MyDerivedShape.Fill = Brushed.Red

这会产生巨大的后果,因为它破坏了我放置的绑定!

它与“依赖属性值优先级”有关,这是我根本不知道的主题。

因此,如果将 ViewModel 属性绑定到依赖属性,则永远不应该直接设置依赖属性。如果您这样做,您的绑定就会丢失!

I answer myself, because I found the answer :

In fact , in another piece of code in the application , I do something like this :

MyDerivedShape.Fill = Brushed.Red

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 !

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