WPF觸發(fā)器在代碼執(zhí)行中作用體現(xiàn)
WPF觸發(fā)器中執(zhí)行代碼操作,如果操作熟練,可以幫助我們輕松實現(xiàn)許多功能。不過對于新手來說,他們還是需要從實戰(zhàn)中去積累經(jīng)驗。#t#
DependencyProperty.RegisterAttached方法允許用戶給控件/窗體等定義自己的依賴屬性,其包含的CallBack參數(shù)可以允許執(zhí)行某個特定方法。這允許我們在Trigger中去調用特定的事件處理。
其實嚴格的說WPF觸發(fā)器和Trigger不太有關系,因為這相當于我們給某個對象添加了自定義屬性并執(zhí)行某些事件。但trigger可以恰恰利用這個好處來簡介的執(zhí)行業(yè)務邏輯:
- public static readonly DependencyProperty
SomethingHappenedProperty =
DependencyProperty.RegisterAttached
("SomethingHappened", typeof(bool),
typeof(Window1), new PropertyMetadata
(false, new PropertyChangedCallback
(SomethingHappened))); - public bool GetSomethingHappened
(DependencyObject d) - {
- return (bool)d.GetValue
(SomethingHappenedProperty); - }
- public void SetSomethingHappened
(DependencyObject d, bool value) - {
- d.SetValue(SomethingHappened
Property, value); - }
- public static void SomethingHappened
(DependencyObject d, Dependency
PropertyChangedEventArgs e) - {
- //do something here
- }
以上就是WPF觸發(fā)器執(zhí)行代碼的相關操作。