如何将自定义属性添加到 WPF 用户控件 [英] How to add Custom Properties to WPF User Control

查看:41
本文介绍了如何将自定义属性添加到 WPF 用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有自己的用户控件,包括一些按钮等.

I've my own User Control including a few buttons and etc.

我使用此代码将该 UC 带到屏幕上.

I use this code to bring that UC to screen.

<AppUI:XXXX x:Name="ucStaticBtns" HorizontalAlignment="Left" Margin="484,0,0,0" VerticalAlignment="Top" Width="68" />

我向 XXXX 用户控件添加了两个属性,如 Property1 和 Property2.并用

I've added two property like Property1 and Property2 to XXXX user control. And changed my code with

<AppUI:XXXX x:Name="ucStaticBtns" HorizontalAlignment="Left" Margin="484,0,0,0" VerticalAlignment="Top" Width="68" Property1="False" Property2="False"/>

当我将这 2 个参数添加到 XAML 页面时,系统会抛出一个异常,例如 成员 'Property1' 无法识别或无法访问"

When I add this 2 parameters to XAML page, system throws an exception like "The member 'Property1' is not recognized or is not accesiable"

这是我的 UC 代码.

Here is my UC code.

 public partial class XXXX : UserControl
    {
        public event EventHandler CloseClicked;
        public event EventHandler MinimizeClicked;
        //public bool ShowMinimize { get; set; }
        public static DependencyProperty Property1Property;
        public static DependencyProperty Property2Property;
        public XXXX()
        {
            InitializeComponent();
        }

        static XXXX()
        {
            Property1Property = DependencyProperty.Register("Property1", typeof(bool), typeof(XXXX));
            Property2Property = DependencyProperty.Register("Property2", typeof(bool), typeof(XXXX));
        }

        public bool Property1
        {
            get { return (bool)base.GetValue(Property1Property); }
            set { base.SetValue(Property1Property, value); }
        }

        public bool Property2
        {
            get { return (bool)base.GetValue(Property2Property); }
            set { base.SetValue(Property2Property, value); }
        }
}

你能帮我做吗?非常感谢!

Can you help me with doing that? Thank you so much!

推荐答案

您可以将此声明用于 DependencyProperties:

You can use this declaration for your DependencyProperties:

public bool Property1
{
    get { return ( bool ) GetValue( Property1Property ); }
    set { SetValue( Property1Property, value ); }
}

// Using a DependencyProperty as the backing store for Property1.  
// This enables animation, styling, binding, etc...
public static readonly DependencyProperty Property1Property 
    = DependencyProperty.Register( 
          "Property1", 
          typeof( bool ), 
          typeof( XXXX ), 
          new PropertyMetadata( false ) 
      );

如果您键入propdp",然后键入 TabTab,则可以在 Visual Studio 中找到此代码段.您需要填写 DependencyProperty 的类型、DependencyProperty 的名称、包含它的类以及该 DependencyProperty 的默认值(在我的示例中,我将 false 设为默认值).

This snippet can be found in Visual Studio if you type "propdp" and then TabTab. You'll need to fill the DependencyProperty's type, the name of the DependencyProperty, the class that contains it and the default value for that DependencyProperty (in my example, I put false as default).

这篇关于如何将自定义属性添加到 WPF 用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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