我有一個自定義控件。有一個堆疊面板與按鍵和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