WPF DependencyProperty 自定义依赖项属性的问题

bird_3333 2010-05-12 03:02:37
我参考http://msdn.microsoft.com/zh-cn/partners/system.windows.dependencyproperty(VS.95).aspx
定义名为LabelText的用户控件 自定义属性名为LabelName


public partial class LabelText : UserControl
{
public static readonly DependencyProperty LabelTextProperty =
DependencyProperty.Register(
"LabelName", typeof(String),
typeof(LabelText), null
);
public String LabelName
{
get { return (String)GetValue(LabelTextProperty); }
set { SetValue(LabelTextProperty, value); }
}
public LabelText()
{
InitializeComponent();
//this.LabelName始终为NULL

}
}

我是使用如下方式引用该控件并初始值的
<....
xmlns:common="clr-namespace:CommonControl"
>
<common:LabelText LabelName="道具名称"></common:LabelText>

但在我的LabelText类的构造中无法得到LabelName为“道具名称” 它始终为NULL.
为何??????????????
...全文
778 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
sharpe 2011-04-30
  • 打赏
  • 举报
回复
我也遇到这个问题,,是怎么回事?
hbdxwudi 2010-05-13
  • 打赏
  • 举报
回复
bird_3333 2010-05-12
  • 打赏
  • 举报
回复
改为这样,可以通过
<common:LabelText LabelName="道具名称"></common:LabelText>
赋值了,可如何实现在属性窗口编辑自定义属性的时候 自定义控件可以及时表现出来改变呢?
public partial class LabelText : UserControl
{
#region LabelValue
public static readonly DependencyProperty LabelValueProperty =
DependencyProperty.Register(
"LabelValue", typeof(String),
typeof(LabelText),
new PropertyMetadata(null, new PropertyChangedCallback(LabelValueChange))
);
#endregion
#region TextBoxValue
public static readonly DependencyProperty TextBoxValueProperty =
DependencyProperty.Register(
"TextBoxValue", typeof(String),
typeof(LabelText),
new PropertyMetadata(null, new PropertyChangedCallback(TextBoxValueChange))
);
#endregion
public String LabelValue
{
get { return (String)GetValue(LabelValueProperty); }
set { SetValue(LabelValueProperty, value); }
}
public String TextBoxValue
{
get { return (String)GetValue(TextBoxValueProperty); }
set { SetValue(TextBoxValueProperty, value); }
}
private Label mLabel = new Label();
private TextBox mTextBox = new TextBox();
public LabelText()
{
InitializeComponent();
Stage.Children.Add(mLabel);
Stage.Children.Add(mTextBox);
}

public static void LabelValueChange(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
LabelText labelText = o as LabelText;
labelText.mLabel.Content = labelText.LabelValue;
}
public static void TextBoxValueChange(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
LabelText labelText = o as LabelText;
labelText.mTextBox.Text = labelText.TextBoxValue;
}
}

13,347

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET技术前瞻
社区管理员
  • .NET技术前瞻社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧