TextBlock
不提供用戶對文本內容的編輯,因爲它只是一個輕量級的控件來顯示文本(在windows store中,如在Windows Phone中)。由於其內容只能以編程方式進行更改,因此在更改時不會發生任何事件。您可以在TextBlock
here上看到活動。在Windows應用商店應用中,用戶可編輯文本仍然有一個TextBox
控件。這has a TextChanged
event。
如果您確實需要知道Text
屬性何時更新,您可以設置綁定到該依賴項屬性。您可以創建更改處理提供您通知自己的自定義依賴項屬性:
class MyClass {
public static readonly DependencyProperty MyTextProperty = DependencyProperty.Register("MyText", typeof(MyClass), typeof(string), new PropertyMetadata(null, OnMyTextChanged));
public static void OnMyTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
// Do something
}
}
然後,你需要的屬性某處綁定:
// myObj something of type MyClass
obj.SetBinding(textBlock, new Binding { Source = myObj, Path = new PropertyPath("MyText") });
感謝您的回答,已經我使用文本塊作爲定製文本框在我的App.Can你給我更多的細節或任何教程綁定到依賴屬性。 – DesertRiver
已更新以顯示使用這種綁定的示例。 Windows應用商店應用上的任何圖書都應該包含創建並綁定到依賴項屬性。 MSDN在這裏:http://msdn.microsoft.com/en-us/library/windows/apps/hh920267.aspx –