WPF - Triggers

使用control自由的property作为trigger
ContractedBlock.gif ExpandedBlockStart.gif View Code
 
      
< ToggleButton Width ="30" Height ="30" >
< ToggleButton.Style >
< Style TargetType ="ToggleButton" >
< Style.Triggers >
< Trigger Property ="IsChecked" Value ="true" >
< Setter Property ="Content" >
< Setter.Value >
< Border BorderThickness ="1" BorderBrush ="Blue" >
< Image Source ="LimitChecked.ico" />
Border >
Setter.Value >
Setter >
Trigger >
< Trigger Property ="IsChecked" Value ="false" >
< Setter Property ="Content" >
< Setter.Value >
< Image Source ="LimitUnChecked.ico" />
Setter.Value >
Setter >
Trigger >
Style.Triggers >
Style >
ToggleButton.Style >
ToggleButton >
-
使用其他control的property作为trigger
ContractedBlock.gif ExpandedBlockStart.gif View Code
 
      
< StackPanel HorizontalAlignment ="Center" VerticalAlignment ="Center" >

< CheckBox x:Name ="theCB" Content ="Enable the TextBox" />

< TextBox >
< TextBox.Style >
< Style TargetType ="TextBox" >
< Style.Triggers >
< DataTrigger Binding =" {Binding ElementName=theCB, Path=IsChecked} " Value ="True" >
< Setter Property ="IsEnabled" Value ="True" />
DataTrigger >
< DataTrigger Binding =" {Binding ElementName=theCB, Path=IsChecked} " Value ="False" >
< Setter Property ="IsEnabled" Value ="False" />
DataTrigger >
Style.Triggers >
Style >
TextBox.Style >
TextBox >

StackPanel >
-
使用ViewModel中的property作为trigger
ContractedBlock.gif ExpandedBlockStart.gif View Code
 
      
< StackPanel HorizontalAlignment ="Center" VerticalAlignment ="Center" >

< CheckBox IsChecked =" {Binding IsRed} " Content ="IsRed" />

< TextBlock >
< TextBlock.Style >
< Style >
< Style.Triggers >
< DataTrigger Binding =" {Binding Path=IsRed} " Value ="True" >
< Setter Property ="TextBlock.Background" Value ="Red" />
DataTrigger >
< DataTrigger Binding =" {Binding Path=IsRed} " Value ="False" >
< Setter Property ="TextBlock.Background" Value ="Black" />
DataTrigger >
Style.Triggers >
Style >
TextBlock.Style >
TextBlock >
StackPanel >
-
tbd...

转载于:https://www.cnblogs.com/yapzhang/archive/2010/09/01/1814519.html

你可能感兴趣的:(WPF - Triggers)