使依赖项对象属性可绑定为静态资源? [英] Make dependency object properties bindable as a static resource?

查看:51
本文介绍了使依赖项对象属性可绑定为静态资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使依赖对象属性的数组可绑定,以便以后作为静态资源进行绑定?

How to make an array of dependency object properties bindable for later binding as a static resource?

我现在拥有的代码,看来我的 DependencyObject 绕过了依赖属性系统...

The code I have now, it seems that my DependencyObject bypasses the dependency property system...

我有以下课程:

public class ValueMarker : DependencyObject
{
    public static readonly DependencyProperty BrushProperty = DependencyProperty.Register("Brush", typeof(Brush), typeof(ValueMarker), new FrameworkPropertyMetadata(Brushes.Aqua));
    public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(ValueMarker), new FrameworkPropertyMetadata(0d));
    public static readonly DependencyProperty OffsetProperty = DependencyProperty.Register("Offset", typeof(double), typeof(ValueMarker), new FrameworkPropertyMetadata(0d));

    public Brush Brush
    {
        get { return (Brush)GetValue(BrushProperty); }
        set { SetValue(BrushProperty, value); }
    }

    public double Offset
    {
        get { return (double)GetValue(OffsetProperty); }
        set { SetValue(OffsetProperty, value); }
    }

    public double Value
    {
        get { return (double)GetValue(ValueProperty); }
        set { SetValue(ValueProperty, value); }
    }
}

在XAML中,我创建了包含这些绑定的资源数组,如下所示:

In the XAML, I create a resource array of these with some bindings like so:

        <x:Array Type="my:ValueMarker" x:Key="plainMarks">
            <my:ValueMarker Brush="Red" Offset="-5" Value="{Binding Path=...}" />
            <my:ValueMarker Brush="Orange" Offset="-5" Value="{Binding Path=...}"/>
            <my:ValueMarker Brush="Orange" Offset="-5" Value="{Binding Path=...}"/>
            <my:ValueMarker Brush="Red" Offset="-5" Value="{Binding Path=...}" />
        </x:Array>

在调试绑定时,我注意到应该删除DP的setter,XAML会显示一条错误,指出该属性丢失.据我了解,XAML使用DP系统分配值,从而启用绑定.在这种情况下,如果XAML期望使用正常"属性,则无法进行绑定.任何人都可以启发我如何使它起作用?

While debugging the bindings, I've noticed that should I remove the setter for the DP, the XAML would display an error saying the property is missing. It was my understanding that XAML uses DP system to assign value thus enabling binding. In this case, if the XAML expect a 'normal' property, binding is impossible. Anyone can enlighten me on how can I make it work?

推荐答案

无法在此处绑定ValueMarkers的原因是:

The reason you cannot bind your ValueMarkers here is because:

1.它们不在您的窗口/用户控件的VisualTree中.

1.They are not in the VisualTree of your window/usercontrol.

2.即使它们不是Visual Tree的一部分,它们也不是可以继承DataContext的Type对象.

2.They are not object of Type that can inherit DataContext even if they are not part of Visual Tree.

因此,为了使您的ValueMarkers绑定到View DataContext中的属性,首先,您必须像下面这样从Freezable类派生它们:

So in order to make your ValueMarkers bind to the properties in the View DataContext, first of all you will have to derive them from Freezable class like below:

 public class ValueMarker : Freezable
    {
        //All your Dependency Properties comes here//

        protected override Freezable CreateInstanceCore()
        {
            return new ValueMarker();
        }
    }

完成此操作后,您可以像下面这样简单地绑定对象:

After doing this you can simply bind your object like below:

   <my:ValueMarker x:Key="vm1" Brush="Orange" Offset="-5" Value="{Binding Path=Text1}"/>

此处Text1是Windows/用户控件DataContext中的属性

Here Text1 is property in Windows/usercontrols DataContext

然后您可以将该资源用作:

Then you can use this resource as:

  <TextBox Text="{Binding Value, Source={StaticResource vm1}, StringFormat=F2}"/>

类似地,您可以为其他ValueMarker创建资源以在绑定中使用它们.

Similarly you can create resource for other ValueMarkers to use them in binding.

您将无法通过创建x:Array进行绑定,因为x:Array不会位于visualtree中并且不会继承DataContext,因此其元素也无法访问它.

You will not be able to bind by creating the x:Array as simply x:Array not lies in visualtree and does not inherit DataContext hence its elements also have no access to it.

如果仍然要使用其元素应支持绑定的集合,则需要创建自己的集合类,该集合类应继承Freezable并公开DependancyProperty来捕获DataContext并将其设置在子元素上.

If you still want to use the collection whose element should support binding, then you will need to create your own collection class that should inherit Freezable and exposes DependancyProperty to capture the DataContext and set it on child elements also.

这篇关于使依赖项对象属性可绑定为静态资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆