如何向WPF用户控件添加自定义属性?

时间:2022-09-02 13:25:08

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

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

I use this code to bring that UC to screen.

我用这段代码把UC带到屏幕上。

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

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

我向XXXX用户控件添加了两个属性,比如Property1和Property2。并改变了我的代码

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

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

当我向XAML页面添加这两个参数时,系统会抛出一个异常,如“成员'Property1'无法识别或无法接受”

Here is my UC code.

这是我的UC代码。

 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!

你能帮我做那件事吗?谢谢你这么多!

2 个解决方案

#1


21  

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

This snippet can be found in Visual Studio if you type "propdp" and then Tab twice. 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.

如果您输入“propdp”,然后在选项卡上重复输入两次,就可以在Visual Studio中找到这个片段。您将需要填充DependencyProperty的类型、DependencyProperty的名称、包含它的类以及该DependencyProperty的默认值(在我的示例中,我将false作为默认值)。

#2


1  

You may not have declared your DependencyPropertys correctly. You can find out full details about how to create DependencyPropertys in the Dependency Properties Overview page on MSDN, but in short, they look something like this (taken from the linked page):

您可能没有正确地声明您的受抚养财产。您可以在MSDN上的依赖属性概述页面中找到关于如何创建DependencyPropertys的详细信息,但是简而言之,它们看起来是这样的(从链接页面中获取):

public static readonly DependencyProperty IsSpinningProperty = 
    DependencyProperty.Register(
    "IsSpinning", typeof(Boolean),
...
    );

public bool IsSpinning
{
    get { return (bool)GetValue(IsSpinningProperty); }
    set { SetValue(IsSpinningProperty, value); }
}

You can find further help in the DependencyProperty Class page on MSDN.

您可以在MSDN上找到进一步的帮助。

#1


21  

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

This snippet can be found in Visual Studio if you type "propdp" and then Tab twice. 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.

如果您输入“propdp”,然后在选项卡上重复输入两次,就可以在Visual Studio中找到这个片段。您将需要填充DependencyProperty的类型、DependencyProperty的名称、包含它的类以及该DependencyProperty的默认值(在我的示例中,我将false作为默认值)。

#2


1  

You may not have declared your DependencyPropertys correctly. You can find out full details about how to create DependencyPropertys in the Dependency Properties Overview page on MSDN, but in short, they look something like this (taken from the linked page):

您可能没有正确地声明您的受抚养财产。您可以在MSDN上的依赖属性概述页面中找到关于如何创建DependencyPropertys的详细信息,但是简而言之,它们看起来是这样的(从链接页面中获取):

public static readonly DependencyProperty IsSpinningProperty = 
    DependencyProperty.Register(
    "IsSpinning", typeof(Boolean),
...
    );

public bool IsSpinning
{
    get { return (bool)GetValue(IsSpinningProperty); }
    set { SetValue(IsSpinningProperty, value); }
}

You can find further help in the DependencyProperty Class page on MSDN.

您可以在MSDN上找到进一步的帮助。