WPF Trigger (Trigger、DataTrigger、EventTrigger、MultiTrigger、MultiDataTrigger)
参考
- 豆包
- https://www.cnblogs.com/hayasi/p/7102451.html
- https://learn.microsoft.com/zh-cn/dotnet/desktop/wpf/events/routed-events-overview
- https://learn.microsoft.com/zh-cn/dotnet/api/system.windows.controls.primitives.buttonbase?view=windowsdesktop-9.0
环境
软件/系统 | 版本 | 说明 |
---|---|---|
Windows | windows 10 专业版 22H2 64 位操作系统, 基于 x64 的处理器 | |
Microsoft Visual Studio | Community 2022 (64 位) - Current 版本 17.13.6 | |
.NET Framework | 4.8 |
预览
部分代码
- MainWindow.xaml
<Window x:Class="WPFInputBinding.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WPFInputBinding" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:vm="clr-namespace:WPFInputBinding.ViewModel" Title="MainWindow" Width="800" Height="450" mc:Ignorable="d"> <Window.DataContext> <vm:MainWindowVM /> </Window.DataContext> <StackPanel> <TextBlock Text="属性触发器" /> <Button> <Button.Content> 按钮 </Button.Content> <Button.Style> <Style TargetType="{x:Type Button}"> <Style.Triggers> <!-- 属性触发器 --> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Foreground" Value="Red" /> <Setter Property="BorderThickness" Value="2" /> </Trigger> </Style.Triggers> </Style> </Button.Style> </Button> <TextBlock Text="鼠标在按钮上面就会让按钮文本变红色" /> <TextBlock Padding="0,10,0,0" Text="事件触发器" /> <Button> <Button.Content> 按钮 </Button.Content> <Button.Style> <Style TargetType="{x:Type Button}"> <Setter Property="FontSize" Value="12" /> <Style.Triggers> <!-- 事件触发器 --> <EventTrigger RoutedEvent="MouseEnter"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetProperty="FontSize" To="28" Duration="0:0:0.3" /> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> <EventTrigger RoutedEvent="MouseLeave"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetProperty="FontSize" To="12" Duration="0:0:0.3" /> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> </Style.Triggers> </Style> </Button.Style> </Button> <TextBlock Text="鼠标经过按钮,则按钮的字体会变大" /> <TextBlock Padding="0,10,0,0" Text="数据触发器" /> <TextBox> <TextBox.Text> 123 </TextBox.Text> <TextBox.Style> <Style TargetType="{x:Type TextBox}"> <Style.Triggers> <Trigger Property="Text" Value="123"> <Setter Property="Foreground" Value="Red" /> </Trigger> <Trigger Property="Text" Value="456"> <Setter Property="Foreground" Value="Blue" /> </Trigger> <Trigger Property="Text" Value="123456"> <Setter Property="Foreground" Value="Yellow" /> </Trigger> </Style.Triggers> </Style> </TextBox.Style> </TextBox> <TextBlock Text="输入123变红色,输入456变蓝色,输入123456变黄色" /> <StackPanel Orientation="Horizontal"> <RadioButton x:Name="sexBoy" Content="男" IsChecked="True" /> <RadioButton x:Name="sexGirl" Content="女" /> </StackPanel> <TextBox> <TextBox.Text> 选中男的变绿色,选中女的变红色。 </TextBox.Text> <TextBox.Style> <Style TargetType="{x:Type TextBox}"> <Style.Triggers> <!-- 数据触发器 --> <DataTrigger Binding="{Binding ElementName=sexBoy, Path=IsChecked}" Value="True"> <Setter Property="Foreground" Value="Green" /> </DataTrigger> <DataTrigger Binding="{Binding ElementName=sexGirl, Path=IsChecked}" Value="True"> <Setter Property="Foreground" Value="Red" /> </DataTrigger> </Style.Triggers> </Style> </TextBox.Style> </TextBox> <TextBlock Padding="0,10,0,0" Text="多条件数据触发器" /> <StackPanel Orientation="Horizontal"> <CheckBox x:Name="game01" Content="游戏1" /> <CheckBox x:Name="game02" Content="游戏2" /> </StackPanel> <TextBlock> <TextBlock.Text> 选中游戏1和游戏2才可以变色本文本 </TextBlock.Text> <TextBlock.Style> <Style TargetType="{x:Type TextBlock}"> <Style.Triggers> <MultiDataTrigger> <MultiDataTrigger.Conditions> <Condition Binding="{Binding ElementName=game01, Path=IsChecked}" Value="True" /> <Condition Binding="{Binding ElementName=game02, Path=IsChecked}" Value="True" /> </MultiDataTrigger.Conditions> <Setter Property="Foreground" Value="Red" /> </MultiDataTrigger> </Style.Triggers> </Style> </TextBlock.Style> </TextBlock> <TextBlock Padding="0,10,0,0" Text="多条件属性触发器" /> <TextBlock> <TextBlock.Text> 鼠标在文本上面就会变色 </TextBlock.Text> <TextBlock.Style> <Style TargetType="{x:Type TextBlock}"> <Style.Triggers> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsEnabled" Value="True" /> <Condition Property="IsMouseOver" Value="True" /> </MultiTrigger.Conditions> <Setter Property="Foreground" Value="Yellow" /> </MultiTrigger> </Style.Triggers> </Style> </TextBlock.Style> </TextBlock> </StackPanel> </Window>
博 主 :夏秋初
地 址 :https://www.cnblogs.com/xiaqiuchu/p/18902615
如果对你有帮助,可以点一下 推荐 或者 关注 吗?会让我的分享变得更有动力~
转载时请带上原文链接,谢谢。
地 址 :https://www.cnblogs.com/xiaqiuchu/p/18902615
如果对你有帮助,可以点一下 推荐 或者 关注 吗?会让我的分享变得更有动力~
转载时请带上原文链接,谢谢。