【问题标题】:How to add Custom Properties to WPF User Control如何将自定义属性添加到 WPF 用户控件
【发布时间】:2014-09-17 15:46:22
【问题描述】:

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

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

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

我在 XXXX 用户控件中添加了两个属性,如 Property1 和 Property2。并用

更改了我的代码
<AppUI:XXXX x:Name="ucStaticBtns" HorizontalAlignment="Left" Margin="484,0,0,0" VerticalAlignment="Top" Width="68" Property1="False" Property2="False"/>

当我将这 2 个参数添加到 XAML 页面时,系统会抛出类似 “成员 'Property1' 无法识别或无法访问”的异常

这是我的 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); }
        }
}

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

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    您可以将此声明用于您的 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 中找到此 sn-p。您需要填写 DependencyProperty 的类型、DependencyProperty 的名称、包含它的类以及该 DependencyProperty 的默认值(在我的示例中,我将 false 作为默认值)。

    【讨论】:

    • VS 使生成的代码片段(通过propdp)适应我的修改的方式很神奇。您是否知道显示更多此内容的“备忘单”?
    • 在花了几天时间解决数据上下文问题之后,我退后一步,重新开始使用这个简单的示例。纯金。
    • 令我惊讶的是,XAML 没有办法做到这一点。我会认为 或类似的东西会是一种更简洁的开发控件的方式。
    【解决方案2】:

    您可能没有正确声明您的DependencyPropertys。您可以在 MSDN 上的 Dependency Properties Overview 页面中找到有关如何创建 DependencyPropertys 的完整详细信息,但简而言之,它们看起来像这样(取自链接页面):

    public static readonly DependencyProperty IsSpinningProperty = 
        DependencyProperty.Register(
        "IsSpinning", typeof(Boolean),
    ...
        );
    
    public bool IsSpinning
    {
        get { return (bool)GetValue(IsSpinningProperty); }
        set { SetValue(IsSpinningProperty, value); }
    }
    

    您可以在 MSDN 上的 DependencyProperty Class 页面中找到更多帮助。

    【讨论】:

      猜你喜欢
      • 2011-03-12
      • 1970-01-01
      • 1970-01-01
      • 2013-08-09
      • 2014-08-11
      • 1970-01-01
      • 2011-06-27
      • 1970-01-01
      • 2010-11-12
      相关资源
      最近更新 更多