【问题标题】:Make dependency object properties bindable as a static resource?使依赖对象属性可绑定为静态资源?
【发布时间】:2013-10-09 06:41:39
【问题描述】:

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

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

我有以下课程:

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 中,我创建了一个资源数组,其中包含一些绑定,如下所示:

        <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 的设置器,XAML 将显示一个错误,指出该属性丢失。我的理解是 XAML 使用 DP 系统来分配值从而启用绑定。在这种情况下,如果 XAML 需要“正常”属性,则无法进行绑定。任何人都可以告诉我如何使它工作?

【问题讨论】:

    标签: c# wpf xaml dependency-properties dependencyobject


    【解决方案1】:

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

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

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

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

     public class ValueMarker : Freezable
        {
            //All your Dependency Properties comes here//
    
            protected override Freezable CreateInstanceCore()
            {
                return new ValueMarker();
            }
        }
    

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

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

    这里 Text1 是 Windows/usercontrols DataContext 中的属性

    那么你可以将这个资源用作:

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

    同样,您可以为其他 ValueMarker 创建资源以在绑定中使用它们。

    您将无法通过创建 x:Array 来进行绑定,因为 x:Array 不位于可视树中,也不继承 DataContext,因此其元素也无法访问它。

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

    【讨论】:

    • 感谢您的回复。这些信息让我重新思考了我的设计,我还发现了绑定代理方法,所以我想我会继续这样做。
    猜你喜欢
    • 1970-01-01
    • 2011-08-19
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 2015-10-17
    • 1970-01-01
    • 2013-02-07
    相关资源
    最近更新 更多