DependencyProperty值继承 [英] DependencyProperty value inheritance

查看:76
本文介绍了DependencyProperty值继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继承依赖项属性值的最佳方法是什么.例如,我有以下代码,但无法正常工作:

What is the best way to inherit Dependency Properties values. For example I have this code and it doesn''t work correctly:

 // UserControl1 contains only TextBox 
public partial class UserControl1 : UserControl
 {
     public static readonly DependencyProperty TextProperty = TextBox.TextProperty.AddOwner(typeof(UserControl1), new FrameworkPropertyMetadata("any text";, FrameworkPropertyMetadataOptions.Inherits, (d,o) => Debugger.Break()));

     public string Text
     {
         get { return this.GetValue(UserControl1.TextProperty).ToString(); }
         set { this.SetValue(UserControl1.TextProperty, value);}
     }
     public UserControl1()
     {
         InitializeComponent();
     }
 }



我将UserControl1添加到Window并在Window的XAML文件中设置Text属性.值仅在UserControl1实例中更改,但未更改TextBox.Text.



I added UserControl1 to Window and set Text property in Window''s XAML file. Value changed only in UserControl1 instance but didn''t change TextBox.Text.

推荐答案

您必须继承DependencyObject才能实现此目的,例如


you have to inherit DependencyObject to achieve this like


public class MyTextBox : DependencyObject
{
//
// Dependency property for Text
//
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register(
"Text",
typeof(string),
typeof(MyTextBox));
//
// Change-aware property
//
public string Text
    get { return base.GetValue(TextProperty) as string; }
set { base.SetValue(TextProperty, value); }
}
}




希望这会有所帮助
:)




Hope this helps
:)


这篇关于DependencyProperty值继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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