概述
WPF中定义了五个触发器类:Trigger、MultiTrigger、DataTrigger、multiDataTrigger、EventTrigger。下面我来介绍一下怎么使用这几个触发器的使用方法。
Trriger(单一条件的触发器):
<Page x:Class="StyleTest.Trigger"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Trigger">
<Page.Resources>
<Style x:Key="smallText">
<Setter Property="Control.Foreground" Value="blue"/>
<Setter Property="Control.FontSize" Value="20"/>
<Setter Property="Control.FontFamily" Value="Times new Roman"/>
<Setter Property="Control.FontWeight" Value="Bold"/>
<Style.Triggers> <!--这里使用了触发器,当鼠标移过控件的时候,控件的风格发生改变-->
<Trigger Property="Control.IsMouseOver" Value="True">
<Setter Property="Control.Foreground" Value="red"/>
<Setter Property="Control.FontSize" Value="24"/>
</Trigger>
</Style.Triggers>
</Style>
</Page.Resources>
<StackPanel>
<TextBlock Style="{StaticResource smallText}">旅望因高尽</TextBlock>
<TextBlock Style="{StaticResource smallText}">乡心遇物悲</TextBlock>
<TextBlock Style="{StaticResource smallText}">故林归宿处</TextBlock>
<TextBlock Style="{StaticResource smallText}">一叶下梧桐</TextBlock>
<Button Style="{StaticResource smallText}">旅思</Button>
</StackPanel>
</Page>
MultiTrigger(多条件的触发器):
<Page x:Class="StyleTest.Multitrigger"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Multitrigger">
<Page.Resources>
<Style x:Key="smallText">
<Setter Property="Control.Foreground" Value="blue"/>
<Setter Property="Control.FontSize" Value="20"/>
<Setter Property="Control.FontFamily" Value="Times New Roman"/>
<Setter Property="Control.FontWeight" Value="Bold"/>
<Style.Triggers>
<Trigger Property="Control.IsMouseOver" Value="True">
<Setter Property="Control.Foreground" Value="red"/>
</Trigger>
<MultiTrigger> <!--定义触发器-->
<MultiTrigger.Conditions>
<Condition Property="Control.IsMouseOver" Value="True"/> <!--通过Condition定义触发条件-->
<Condition Property="Button.IsPressed" Value="True"/> <!--通过Condition定义触发条件-->
</MultiTrigger.Conditions>
<Setter Property="Control.Foreground" Value="gray"/>
<Setter Property="Control.FontStyle" Value="Italic"/>
</MultiTrigger>
</Style.Triggers>
</Style>
</Page.Resources>
<StackPanel>
<Button Style="{StaticResource smallText}">床前明月光</Button>
<Button Style="{StaticResource smallText}">疑是地上霜</Button>
<Button Style="{StaticResource smallText}">举头望明月</Button>
<Button Style="{StaticResource smallText}">低头思故乡</Button>
</StackPanel>
</Page>
DataTrigger(数据触发器):DataTrigger和MultiDataTriffer这一对触发器和Trigger和MultiTrigger非常类似。但是DataTrigger多了一个Binding属性,其语法如下:
<DataTrigger Binding="{Binding ElementName=控件名,path=空间中的相应属性}" value="相关属性的值">
如:
<Page x:Class="StyleTest.DataTrigger"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="DataTrigger">
<Page.Resources>
<Style x:Key="smallText">
<Setter Property="Control.Foreground" Value="Black"/>
<Setter Property="Control.FontSize" Value="24"/>
<Setter Property=" Control.FontFamily" Value="Times New Roman"/>
<Setter Property="Control.FontWeight" Value="Regular"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=italicFont, Path=IsChecked}" Value="True"> <!--这里绑定控件的名称,及控件属性-->
<Setter Property=" Control.FontStyle" Value="Italic"/> <!--当该控件触发该事件的时候,则所有控件的字体样式变为斜体-->
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=boldFont, Path=IsChecked}" Value="True">
<Setter Property="Control.FontStyle" Value="Normal"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=redFont, Path=IsChecked}" Value="True">
<Setter Property=" Control.Foreground" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=blueFont, Path=IsChecked}" Value="True">
<Setter Property="Control.Foreground" Value="Blue"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=btnBold, Path=IsPressed}" Value=" True">
<Setter Property="Control.FontSize" Value="12"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Page.Resources>
<StackPanel>
<GroupBox>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<RadioButton Name="italicFont" Grid.Row="0" Grid.Column="0">斜体</RadioButton>
<RadioButton Name="boldFont" Grid.Row="0" Grid.Column="1">正常</RadioButton>
<RadioButton Name="redFont" Grid.Row="1" Grid.Column="0">红色</RadioButton>
<RadioButton Name="blueFont" Grid.Row="1" Grid.Column="1">蓝色</RadioButton>
</Grid>
</GroupBox>
<TextBlock Style="{StaticResource smallText}">床前明月光</TextBlock>
<TextBlock Style="{StaticResource smallText}">疑是地上霜</TextBlock>
<TextBlock Style="{StaticResource smallText}">举头望明月</TextBlock>
<TextBlock Style="{StaticResource smallText}">低头思故乡</TextBlock>
<Button x:Name="btnBold" Style="{StaticResource smallText}">粗体</Button>
</StackPanel>
</Page>
最后
以上就是怕孤独白猫为你收集整理的WPF触发器的使用的全部内容,希望文章能够帮你解决WPF触发器的使用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复