我有一个自定义控件。有一个堆叠面板与按键和TextBlock的在generic.xaml:如何从代码隐藏中更改TextBlock文本?
<StackPanel>
<TextBlock x:Name="StatusText" />
</StackPanel>
然后,我有
public class MyClass : Control
{
// Constructor etc.
public static readonly DependencyProperty StatusTextProperty = DependencyProperty.Register("StatusText", typeof(TextBlock), typeof(MyClass), null);
public TextBlock StatusText
{
get { return (TextBlock)this.GetValue(StatusTextProperty); }
set { SetValue(StatusTextProperty, value); }
}
}
有如果与单击该按钮后,某些逻辑发生。 如何更改TextBloc的Text属性? 我认为我可以做这样的事情
StatusText.SetValue(TextBlock.TextProperty, "Some text here.");
但它总是返回的NullReferenceException(未将对象引用设置到对象的实例。)
我应该对依赖属性或什么用PropertyChangedCallback()我需要吗?我失去了一些东西;-)
如果文本已经绑定了,我想获得一个stackpanel内的textblock的当前值? – Freakishly 2011-06-08 02:24:46