Dependency Property optimisation?

2020-04-19 06:16发布

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>

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>

Code of control:

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

Collection items code:

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

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?

标签: c# .net wpf xaml
1条回答
家丑人穷心不美
2楼-- · 2020-04-19 06:48

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

Instead of setting a local value, you could use the SetCurrentValue method:

public InjectCustomArray()
{
    InitializeComponent();
    SetCurrentValue(MyPropertyProperty, new ICustomType[0]);
}

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

查看更多
登录 后发表回答