c# - 使依赖对象属性可绑定(bind)为静态资源?

标签 c# wpf xaml dependency-properties dependencyobject

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

我现在的代码,似乎我的DependencyObject绕过了依赖属性系统...

我有以下类(class):

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

        <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>

在调试绑定(bind)时,我注意到如果删除 DP 的 setter ,XAML 会显示一条错误消息,指出属性丢失。据我了解,XAML 使用 DP 系统分配值,从而启用绑定(bind)。在这种情况下,如果 XAML 需要“正常”属性,则绑定(bind)是不可能的。任何人都可以启发我如何让它发挥作用?

最佳答案

您不能在此处绑定(bind)您的 ValueMarkers 的原因是:

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

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

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

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

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

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

   <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 创建资源以在绑定(bind)中使用它们。

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

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

关于c# - 使依赖对象属性可绑定(bind)为静态资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19144410/

相关文章:

WPF 的命令在快速双击时触发两次

c# - wpf Storyboard死亡

xaml - UWP ComboBox 中的 StackOverflowException

c# - 动态设置属性 WPF 应用程序

C#/XML : Change/Replace data from an XML file via textbox

c# - 需要将字符串解析为 mm :ss not as hh:mm

c# - WCF 服务传递对象缓慢。那是正常的吗?

c# - 解决 SQL 查询的超时错误

c# - 在异步方法主体中获取当前任务实例

c# - WPF WebBrowser-Control 不显示内容