我是靠谱客的博主 甜蜜魔镜,最近开发中收集的这篇文章主要介绍WPF 基础(十六)WPF 四大触发器总结,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

参考:

https://blog.csdn.net/lhx527099095/article/details/8029207

例子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 基础(十六)WPF 四大触发器总结所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部