简体   繁体   English

自定义属性上的WPF动态值

[英]WPF Dynamic value on a custom property

I Have a User Control which contains Expander and few other controls. 我有一个用户控件,其中包含扩展器和其他几个控件。

User control have a custom "XBackground" property which actually sets the background only for the Expander. 用户控件具有自定义的“ XBackground”属性,该属性实际上仅为扩展器设置背景。

    public Brush XBackground
    {
        get
        {
            return expander.Background;
        }
        set
        {
            expander.Background = value;
        }
    }

When I use my user control, background can only be set statically, not dynamically. 使用用户控件时,只能静态设置背景,不能动态设置。 Debugger says that only DependencyProperty can be set via dynamic resource. 调试器说只能通过动态资源设置DependencyProperty。 And here I'm stuck. 在这里我被卡住了。 I tried to register dependency property on my XBackground property but I'm receiving an error saying "A 'DynamicResourceExtension' can only be set on a DependencyProperty of a DependencyObject." 我试图在我的XBackground属性上注册依赖项属性,但收到一条错误消息,说“只能在DependencyObject的DependencyProperty上设置'DynamicResourceExtension'。

Here is my try of registering dependency property: 这是我注册依赖项属性的尝试:

public static readonly DependencyProperty BcgProperty = DependencyProperty.Register("XBackground", typeof(Brush), typeof(Expander));

Minor mistake: 小错误:

public static readonly DependencyProperty BcgProperty = DependencyProperty.Register("XBackground", typeof(Brush), typeof( YourCustomUserControl )); 公共静态只读DependencyProperty BcgProperty = DependencyProperty.Register(“ XBackground”,typeof(Brush),typeof( YourCustomUserControl ));

Full version: 完整版本:

public static readonly DependencyProperty XBackgroundProperty 
            = DependencyProperty.Register("XBackground", typeof(Brush), typeof(YourCustomUserControl));
public Brush XBackground
{
    get { return (Brush) GetValue( XBackgroundProperty ); }
    set { SetValue( XBackgroundProperty, value ); }
}

Owner class in Register is not an Expander, but the name of the class in which your DependencyProperty is registered. Register中的所有者类不是扩展器,而是注册DependencyProperty的类的名称。 Try this: 尝试这个:

    public Brush XBackground
    {
        get { return (Brush)GetValue(XBackgroundProperty); }
        set { SetValue(XBackgroundProperty, value); }
    }

    // Using a DependencyProperty as the backing store for XBackground.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty XBackgroundProperty =
        DependencyProperty.Register("XBackground", typeof(Brush), typeof(/typeof your UserControl goes here/), new PropertyMetadata(null));

You must not use typeof(Expander) , but instead the type of your UserControl as the ownerType argument of the Register method. 您不得使用typeof(Expander) ,而ownerType UserControl的类型用作Register方法的ownerType参数。 In addition you'll also have to call GetValue and SetValue in the property wrapper. 另外,您还必须在属性包装器中调用GetValueSetValue

Besides that, you would also have to a register a PropertyChangedCallback with the property metadata, to get notified about property value changes: 除此之外,您还必须向属性元数据注册一个PropertyChangedCallback ,以获取有关属性值更改的通知:

public partial class YourUserControl : UserControl
{
    ...

    public Brush XBackground
    {
        get { return (Brush)GetValue(XBackgroundProperty); }
        set { SetValue(XBackgroundProperty, value); }
    }

    public static readonly DependencyProperty XBackgroundProperty =
        DependencyProperty.Register(
            "XBackground",
            typeof(Brush),
            typeof(YourUserControl),
            new PropertyMetadata(null, XBackgroundPropertyChanged));

    private static void XBackgroundPropertyChanged(
        DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        var userControl = (YourUserControl)obj;
        userControl.expander.Background = (Brush)e.NewValue;
    }
}

An alternative to the PropertyChangedCallback would be to bind the Expander's Background property to the UserControl's XBackground property in XAML: PropertyChangedCallback的替代方法是将扩展器的Background属性绑定到XAML中的UserControl的XBackground属性:

<UserControl ...>
    ...
    <Expander Background="{Binding XBackground,
        RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"/>
    ...
</UserControl>

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

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