繁体   English   中英

从DataTemplate设置自定义DependencyProperty

[英]Custom DependencyProperty set from DataTemplate

我正在使用具有多个用户定义的依赖项属性的自定义控件。 我遇到了这个问题中描述的相同问题

我的控件是在其构造函数中设置自定义依赖项属性的默认值。 当我在DataTemplate使用控件时,总是使用构造函数中设置的值,即使我尝试在XAML中设置它。

链接问题的答案解释了C#代码中设置的值具有更高的优先级,更好的方法是在依赖项属性的元数据中指定默认值。

就我而言,我无法指定默认值,因为依赖项属性没有一个适用于所有情况的默认值。 默认值取决于另一个属性,因此我必须在创建控件时查找它们,而不是在注册属性时查找它们。

这里有一些代码可以帮助说明我的问题:

public partial class MyControl : UserControl
{
    public static readonly DependencyProperty MyProperty = 
            DependencyProperty.Register(
                "MyProperty",
                typeof(int),
                typeof(MyControl),
                new FrameworkPropertyMetadata(
                    int.MinValue,
                    FrameworkPropertyMetadataOptions.None,
                    new PropertyChangedCallback("OnMyPropertyChanged")));

    public MyControl() : base()
    {
        InitializeComponent();
        this.MyProperty = GetDefaultPropertyValue();
    }

    public int MyProperty
    {
        get { return (int)GetValue(MyProperty); }
        set { SetValue(MyProperty, value); }
    }

    private int GetDefaultPropertyValue()
    {
        // look up the appropriate default based on some other criteria
        return 42;

        // (in reality, the default value for "MyProperty"
        // depends on the value of a "Mode" custom DependencyProperty.
        // this is just hard coded for testing)
    }   
}

XAML用法如下所示:

<!-- View displays 4 (desired) -->
<local:MyControl MyProperty="4" />

<!-- View displays default of 42 (desired) -->
<local:MyControl />

<!-- View displays default of 42 (wanted 4) -->
<DataTemplate x:Key="MyTemplate">
    <local:MyControl MyProperty="4"/>
</DataTemplate>

总结一下:

所需的行为是首先使用XAML的值。 如果未在XAML中指定该值,那么我想回退到控件构造函数中设置的默认值。

如果我只是直接在视图中包含控件,我会得到预期的行为。 如果控件在DataTemplate ,那么我总是在构造函数中获得默认设置(即使数据模板显式设置了另一个值)。

在模板中使用控件时,是否还有其他方法可以指定默认值? 我能想到的唯一选择是将控件分解为几个单独但相似的控件,每个控件都使用一个在依赖属性中注册的默认值(这样就不需要根据Mode属性设置默认值了) )。

在添加小型检查时在OnApplyTemplate设置默认值应解决此问题:

public override void OnApplyTemplate()
{
    base.OnApplyTemplate();

    // Only set the default value if no value is set.
    if (MyProperty == (int)MyPropertyProperty.DefaultMetadata.DefaultValue)
    {
        this.MyProperty = GetDefaultPropertyValue();
    }
}

请注意,尽管这会起作用,但它并不理想,因为通过代码设置属性的值将基本上清除此属性的任何数据绑定。 例如,一旦在代码中调用MyProperty = 42 ,以下绑定将不再有效:

<local:MyControl MyProperty="{Binding SomeProperty}" />

应该可以通过使用SetCurrentValue(MyPropertyProperty, GetDefaultPropertyValue());在保持任何绑定的同时设置值SetCurrentValue(MyPropertyProperty, GetDefaultPropertyValue()); 修改属性而不是MyProperty = GetDefaultPropertyValue() ,但我不确定我是否也喜欢这个。

更好的解决方案

我要做的是引入一个新的只读属性,除了现有的属性,它将作为计算属性。 例如:

private static readonly DependencyPropertyKey MyCalculatedPropertyPropertyKey =
    DependencyProperty.RegisterReadOnly("MyCalculatedProperty", typeof(int), typeof(MyControl),
    new PropertyMetadata(int.MinValue));

public static readonly DependencyProperty MyCalculatedPropertyProperty = MyCalculatedPropertyPropertyKey.DependencyProperty;

public int MyCalculatedProperty
{
    get { return (int)GetValue(MyCalculatedPropertyProperty); }
    private set { SetValue(MyCalculatedPropertyPropertyKey, value); }
}

private static void OnMyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    ((MyControl)d).MyCalculatedProperty = (int)e.NewValue;
}

public MyControl()
    : base()
{
    InitializeComponent();
    MyCalculatedProperty = GetDefaultPropertyValue();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM