Behavior of DependencyProperty With Regards to Multiple FrameworkElement Instances

*爱你&永不变心* 提交于 2020-01-10 04:31:04

问题


So I tried using DependencyProperty to solve my issues with regards to passing the local ViewModel across child Views. However a question popped in my head.

For example I need to make multiple instances of a certain FrameworkElement, say, a UserControl. That UserControl has a DependencyProperty defined. As stated in books, a dependency property instance should be static and readonly. How would the DependencyProperty work in that kind of scenario? Would it work the same way as a conventional UserControl property, or whatever object instance you pass to the DependencyProperty, it'll be passed across all instances of the said UserControl?


回答1:


Yes, it will operate as a normal property. If you need a property for a specific control, that is one property for a single control, you can use just dependency property. They will be passed through all the instances of the class. But if you want the property on many controls, then should use the attached dependency property, which will be available to all members within a certain namespace. Properties, such as: Canvas.Top, DockPanel.Dock are attached DependencyProperty.

Sample of attached dependency properties:

public class MyDependencyClass : DependencyObject
{
    public static readonly DependencyProperty IsSelectedProperty;

    public static void SetIsSelected(DependencyObject DepObject, Boolean value)
    {
        DepObject.SetValue(IsSelectedProperty, value);
    }

    public static Boolean GetIsSelected(DependencyObject DepObject)
    {
        return (Boolean)DepObject.GetValue(IsSelectedProperty);
    }

    private static bool IsSelectedValid(object Value)
    {
        if (Value.GetType() == typeof(bool))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    static MyDependencyClass()
    {
        FrameworkPropertyMetadata MetaData = new FrameworkPropertyMetadata((Boolean)false);

        IsSelectedProperty = DependencyProperty.RegisterAttached("IsSelected",
                                                            typeof(Boolean),
                                                            typeof(MyDependencyClass),
                                                            MetaData,
                                                            new ValidateValueCallback(IsSelectedValid));
    }
}

They also contain useful callback's like OnPropertyChangedCallback, ValidateValueCallback which can be placed in an additional logic.

These properties are also available in XAML. Add "local" namespace:

xmlns:local="clr-namespace:SampleApp"

Define for element's:

<Button Name="Button1" local:MyDependencyClass.IsSelected="True" />
<Button Name="Button2" local:MyDependencyClass.IsSelected="False" />

...

<ListBoxItem Name="Sample" local:MyDependencyClass.IsSelected="True" />

Access to property in triggers:

<Trigger Property="local:MyDependencyClass.IsSelected" Value="True">
    <Setter Property="Background" Value="Green" />
</Trigger>

Work with attached dependency properties in code:

if (CurrentButtonName == MyButton.Name)
{
    MyDependencyClass.SetIsSelected(CurrentButton, true);
}
else
{
    MyDependencyClass.SetIsSelected(CurrentButton, false);
}

For more info see: http://msdn.microsoft.com/en-us/library/ms749011.aspx



来源:https://stackoverflow.com/questions/16963221/behavior-of-dependencyproperty-with-regards-to-multiple-frameworkelement-instanc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!