本文介绍了DependencyProperty的行为与问候多个实例FrameworkElement的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图用的DependencyProperty 来解决我的问候问题到儿童意见通过当地视图模型。然而,一个问题突然出现在我的脑海。

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.

例如我需要有一定的 FrameworkElement的,说多实例,一个用户控件。这用户控件都有一个定义的的DependencyProperty 。正如书中所说,一个依赖属性的实例应该是静态只读。怎么会在那种情况下的的DependencyProperty 工作?将它的工作方式与以往相同的用户控件财产,或任何对象实例,你传递给的DependencyProperty ,它的会跨的所有实例传递上述用户控件

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?

推荐答案

是的,它将作为一个正常的属性进行操作。如果你需要一个特定的控制属性,这是一个控制一个属性,你可以只使用依赖属性。他们将通过类的所有实例进行传递。但是如果你想在许多控件属性,那么应该使用的连接的依赖项属性,这会在一定命名空间内提供给所有成员。属性,如:Canvas.Top,DockPanel.Dock是的连接的的DependencyProperty。附加的依赖属性。

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.

示例:

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));
    }
}



它们还含有有益回调的像的 OnPropertyChangedCallback ValidateValueCallback 的可被放置在一个附加的逻辑

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

这些性质,可以在XAML提供。添加本地命名空间:

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

xmlns:local="clr-namespace:SampleApp"

定义元素的:

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

...

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

在触发访问属性:

<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

这篇关于DependencyProperty的行为与问候多个实例FrameworkElement的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 01:50