WPF觸發(fā)器在代碼執(zhí)行中作用體現(xiàn)
WPF觸發(fā)器中執(zhí)行代碼操作,如果操作熟練,可以幫助我們輕松實(shí)現(xiàn)許多功能。不過(guò)對(duì)于新手來(lái)說(shuō),他們還是需要從實(shí)戰(zhàn)中去積累經(jīng)驗(yàn)。#t#
DependencyProperty.RegisterAttached方法允許用戶(hù)給控件/窗體等定義自己的依賴(lài)屬性,其包含的CallBack參數(shù)可以允許執(zhí)行某個(gè)特定方法。這允許我們?cè)赥rigger中去調(diào)用特定的事件處理。
其實(shí)嚴(yán)格的說(shuō)WPF觸發(fā)器和Trigger不太有關(guān)系,因?yàn)檫@相當(dāng)于我們給某個(gè)對(duì)象添加了自定義屬性并執(zhí)行某些事件。但trigger可以恰恰利用這個(gè)好處來(lái)簡(jiǎn)介的執(zhí)行業(yè)務(wù)邏輯:
- 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í)行代碼的相關(guān)操作。