WPFの触发器详解

Posted xietianjiao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPFの触发器详解相关的知识,希望对你有一定的参考价值。

技术图片

例子1 简单触发器Triggers——满足简答的条件,触发

<Window x:Class="Styles.SimpleTriggers"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SimpleTriggers" Height="300" Width="300"
>
<Window.Resources>
<Style x:Key="BigFontButton">
<Style.Setters>
<Setter Property="Control.FontFamily" Value="Times New Roman" />
<Setter Property="Control.FontSize" Value="10" />

</Style.Setters>
<Style.Triggers>
<Trigger Property="Control.IsFocused" Value="True">
<Setter Property="Control.Foreground" Value="DarkRed" />
</Trigger>
<!--<Trigger Property="Control.IsMouseOver" Value="True">
<Setter Property="Control.Foreground" Value="LightYellow" />
<Setter Property="Control.FontWeight" Value="Bold" />
</Trigger> 
<Trigger Property="Button.IsPressed" Value="True">
<Setter Property="Control.Foreground" Value="Red" />
</Trigger>-->
</Style.Triggers>
</Style>
</Window.Resources>

<StackPanel Margin="5">
<Button Padding="5" Margin="5"
Style="StaticResource BigFontButton" 
>A Customized Button</Button>
<TextBlock Margin="5">Normal Content.</TextBlock>
<Button Padding="5" Margin="5"
>A Normal Button</Button>
<TextBlock Margin="5">More normal Content.</TextBlock>
<Button Padding="5" Margin="5"
Style="StaticResource BigFontButton" 
>Another Customized Button</Button>
</StackPanel>
</Window>

 


例子2 条件多触发MultiTriggers——满足多条件才触发

<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsFocused" Value="True"></Condition>
<Condition Property="Content" Value="x:Null"></Condition>
</MultiTrigger.Conditions>

<Setter Property="ToolTip" Value="content is null!"></Setter>
</MultiTrigger>

例子3 事件触发器EventTrigger——在特定的路由事件发生时被触发,主要用于动画。
<Style TargetType="ListBoxItem">
<Setter Property="Opacity" Value="0.5" />
<Setter Property="MaxHeight" Value="75" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Trigger.Setters>
<Setter Property="Opacity" Value="1.0" />
</Trigger.Setters>
</Trigger>
<EventTrigger RoutedEvent="Mouse.MouseEnter">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Duration="0:0:0.2"
Storyboard.TargetProperty="MaxHeight"
To="90" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="Mouse.MouseLeave">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Duration="0:0:1"
Storyboard.TargetProperty="MaxHeight" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>

  

 

例字4 数据触发器DataTrigger——根据绑定的数据不同显示不同的内容。

<TreeView Name="_tree" Margin="0" BorderThickness="0" VerticalAlignment="Stretch" Background="Transparent" ItemsSource="Binding Children" >
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="Binding Children">
<Border CornerRadius="0" Margin="1" x:Name="back" MinWidth="70"
Background="Transparent" DataContext="Binding" PreviewMouseMove="TreeItem_PreviewMouseMove">
<StackPanel Orientation="Horizontal" Margin="2">
<Image x:Name="BGimage" Source="/Vdc3D.Coms.DModelEditor;component/Images/item.png" Height="15" Width="15" />
<TextBlock Text="Binding ShowText" Margin="2 0"/>
</StackPanel>
<Border.ContextMenu>
<ContextMenu x:Name="menu" >
<MenuItem Header="Add Directory" x:Name="menu_addDir" Click="AddDir_Click" DataContext="Binding"/>
<MenuItem Header="Add Property" x:Name="menu_addChild" Click="AddChild_Click" DataContext="Binding"/>
<MenuItem Header="Edit" Click="Modify_Click" DataContext="Binding"/>
<MenuItem Header="Delete" Click="Delete_Click" DataContext="Binding"/>
</ContextMenu>
</Border.ContextMenu>
</Border>
<HierarchicalDataTemplate.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="Binding RelativeSource=RelativeSource Mode=FindAncestor,AncestorType=x:Type TreeViewItem,Path=IsExpanded" Value="False"/>
<Condition Binding="Binding IsDir" Value="True"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter TargetName="BGimage" Property="Source" Value="/Vdc3D.Coms.DModelEditor;component/Images/dir.png" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="Binding RelativeSource=RelativeSource Mode=FindAncestor,AncestorType=x:Type TreeViewItem,Path=IsExpanded" Value="True"/>
<Condition Binding="Binding IsDir" Value="True"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter TargetName="BGimage" Property="Source" Value="/Vdc3D.Coms.DModelEditor;component/Images/dir_open.png" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
<DataTrigger Binding="Binding IsDir" Value="True">
<Setter TargetName="menu_addDir" Property="Visibility" Value="Visible"/>
<Setter TargetName="menu_addChild" Property="Visibility" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="Binding IsDir" Value="False">
<Setter TargetName="menu_addDir" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="menu_addChild" Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>

  

以上是关于WPFの触发器详解的主要内容,如果未能解决你的问题,请参考以下文章

WPF: WPF 中的 Triggers 和 VisualStateManager

WPF: WPF 中的 Triggers 和 VisualStateManager

WPF中的触发器(Trigger)

WPF – 使用触发器

WPF触发器(非数据库中的触发器)

WPF 精修篇 事件触发器