依赖财产优化? [英] Dependency Property optimisation?

查看:41
本文介绍了依赖财产优化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我像这样在控件中设置来自xaml的收集值时,似乎可以正确设置值:

When I set collection value from xaml within the control as such it seems to set value correctly:

 <local:InjectCustomArray>
    <local:InjectCustomArray.MyProperty>
        <x:Array Type="local:ICustomType">
            <local:CustomType/>
            <local:CustomType/>
            <local:CustomType/>
        </x:Array>
    </local:InjectCustomArray.MyProperty>
 <local:InjectCustomArray>

如果从样式中设置了值,则在ctor中设置它的初始值时,似乎没有遇到依赖项属性回调:

if value is being set from style it does not appear to be hitting dependency property callback if you set initial value of it in ctor:

   <local:InjectCustomArray>
        <local:InjectCustomArray.Style>
            <Style TargetType="local:InjectCustomArray">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding NonExistingProp}" Value="{x:Null}">
                        <Setter Property="MyProperty">
                            <Setter.Value>
                                <x:Array Type="local:ICustomType">
                                    <local:CustomType/>
                                    <local:CustomType/>
                                    <local:CustomType/>
                                    <local:CustomTypeTwo/>
                                </x:Array>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="Background" Value="Black"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </local:InjectCustomArray.Style>
    </local:InjectCustomArray>

控制代码:

public partial class InjectCustomArray : UserControl
{
    public InjectCustomArray()
    {
        InitializeComponent();
        // If following line is being commented out then setter value in xaml is being set, otherwise not.
        MyProperty = new ICustomType[0];
    }

    public ICustomType[] MyProperty
    {
        get { return (ICustomType[])GetValue(MyPropertyProperty); }
        set { SetValue(MyPropertyProperty, value); }
    }

    public static readonly DependencyProperty MyPropertyProperty =
        DependencyProperty.Register("MyProperty", typeof(ICustomType[]), typeof(InjectCustomArray), new PropertyMetadata(Callback));

    private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
      // This is being hit only once from a ctor.
      // If ctor is commented out then this value is being set from setter correctly.
    }
}

收藏品代码:

public interface ICustomType
{
}
public class CustomType : ICustomType
{
}    
public class CustomTypeTwo : ICustomType
{
}

问题:在相近时间范围内设置DependancyProperty的值时,是否有一些优化?这种行为的原因是什么?

Question: Is there some optimisation within DependancyProperty when values of it is being set within close time range? What is the cause of such behavior?

推荐答案

原因是本地属性值的优先级高于样式设置器中的值.

The cause is that a local property value has higher precedence than a value from a Style Setter.

您可以使用请参见相关属性值优先级有关MSDN的文章,以获取更多详细信息.

See the Dependency Property Value Precedence article on MSDN for more details.

这篇关于依赖财产优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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