【发布时间】:2011-04-23 07:43:56
【问题描述】:
我有一个自定义控件。 generic.xaml 中有一个带有 Button 和 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() 还是我还需要什么?我错过了一些东西;-)
【问题讨论】:
标签: silverlight silverlight-4.0