实例介绍
【实例截图】
【核心代码】
<Window x:Class="WPF_Triggers.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
Title="MainWindow"
Width="650"
Height="500"
WindowStartupLocation="CenterScreen">
<Window.Resources>
<Storyboard x:Key="MouseHoverStoryBoard">
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="EventTriggerBorder" Storyboard.TargetProperty="(FrameworkElement.Width)">
<EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="250" />
</DoubleAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="EventTriggerBorder" Storyboard.TargetProperty="Background.GradientStops[0].Color">
<LinearColorKeyFrame KeyTime="0:0:1" Value="#FF00FF" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="MouseLeaveStoryBoard">
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="EventTriggerBorder" Storyboard.TargetProperty="(FrameworkElement.Width)">
<EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="200" />
</DoubleAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="EventTriggerBorder" Storyboard.TargetProperty="Background.GradientStops[0].Color">
<LinearColorKeyFrame KeyTime="0:0:1" Value="#FFE9DDDA" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="LoadStoryBoard"
AutoReverse="True"
RepeatBehavior="Forever">
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="LoadedBorder" Storyboard.TargetProperty="(UIElement.Opacity)">
<EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="0.4" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
<Style.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Opacity" Value="0.5" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
<ControlTemplate x:Key="ButtonBaseControlTemplate1" TargetType="{x:Type ButtonBase}">
<Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
SnapsToDevicePixels="True">
<ContentPresenter Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding Content}"
ContentStringFormat="{TemplateBinding ContentStringFormat}"
ContentTemplate="{TemplateBinding ContentTemplate}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Microsoft_Windows_Themes:ButtonChrome>
<ControlTemplate.Triggers>
<Trigger Property="IsKeyboardFocused" Value="True">
<Setter TargetName="Chrome" Property="RenderDefaulted" Value="False" />
</Trigger>
<Trigger Property="ToggleButton.IsChecked" Value="True">
<Setter TargetName="Chrome" Property="RenderPressed" Value="False" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<Style x:Key="MulitTriggerButtonStyle" TargetType="Button">
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsPressed" Value="True" />
<Condition Property="Background" Value="BlanchedAlmond" />
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter Property="Foreground" Value="Blue" />
<Setter Property="BorderThickness" Value="5" />
<Setter Property="BorderBrush" Value="Blue" />
</MultiTrigger.Setters>
</MultiTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Margin="0 0 0 5"
HorizontalAlignment="Center"
FontSize="16"
FontWeight="Black"
Text="Event Trigger" />
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*" />
<ColumnDefinition Width="5*" />
</Grid.ColumnDefinitions>
<Border x:Name="EventTriggerBorder"
Width="200"
Height="50"
Margin="20,0,0,0"
HorizontalAlignment="Left"
Cursor="Hand">
<Border.Background>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Offset="0" Color="#FFE9DDDA" />
<GradientStop Offset="1" Color="#FF9F6A1D" />
</LinearGradientBrush>
</Border.Background>
<Border.Triggers>
<EventTrigger RoutedEvent="Mouse.MouseEnter">
<BeginStoryboard Storyboard="{StaticResource MouseHoverStoryBoard}" />
</EventTrigger>
<EventTrigger RoutedEvent="Mouse.MouseLeave">
<BeginStoryboard Storyboard="{StaticResource MouseLeaveStoryBoard}" />
</EventTrigger>
</Border.Triggers>
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
FontWeight="Black"
Text="MouseOver/MouseOut Trigger" />
</Border>
<Border x:Name="LoadedBorder"
Grid.Column="1"
Width="150"
Height="40"
Background="LightBlue"
BorderBrush="Black"
BorderThickness="1">
<Border.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Storyboard="{StaticResource LoadStoryBoard}" />
</EventTrigger>
</Border.Triggers>
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
FontWeight="Black"
Text="Loaded Event Trigger" />
</Border>
</Grid>
<TextBlock Grid.Row="2"
Margin="0 10 0 5"
HorizontalAlignment="Center"
FontSize="16"
FontWeight="Black"
Text="Property Trigger" />
<Grid Grid.Row="3">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="2.5*" />
</Grid.ColumnDefinitions>
<Button x:Name="PropertyTriggerButton"
Grid.Column="0"
Width="160"
Height="40"
Margin="20,0,0,0"
HorizontalAlignment="Left"
Content="IsPressed Property"
Cursor="Hand"
FontWeight="Bold"
Style="{StaticResource ButtonStyle}"
Template="{DynamicResource ButtonBaseControlTemplate1}"
ToolTip="Press To Raise Property Trigger">
<Button.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Offset="0" Color="#FFE4D0D0" />
<GradientStop Offset="1" Color="#FF663434" />
<GradientStop Offset="0.513" Color="#FFA48181" />
</LinearGradientBrush>
</Button.Background>
</Button>
<Button Grid.Column="1"
Width="160"
Height="40"
Content="IsEnabled Property"
IsEnabled="False"
Style="{StaticResource ButtonStyle}" />
<Button Grid.Column="2"
Width="230"
Height="40"
Background="BlanchedAlmond"
Content="MultiTrigger (IsPressed Background)"
Cursor="Hand"
FontWeight="Black"
Style="{StaticResource MulitTriggerButtonStyle}"
Template="{DynamicResource ButtonBaseControlTemplate1}"
ToolTip="Press To Raise Multi-Property Trigger" />
</Grid>
<Grid Grid.Row="4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*" />
<ColumnDefinition Width="5*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"
Grid.Column="0"
Margin="0 10 0 5"
HorizontalAlignment="Center"
FontSize="16"
FontWeight="Black"
Text="Data Trigger" />
<ListBox Grid.Row="1"
Grid.Column="0"
ItemsSource="{Binding ViewItemList}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="0 5 0 0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Image x:Name="viewImage"
Grid.Row="0"
Width="100"
Height="60"
HorizontalAlignment="Center"
Source="{Binding Picture}"
Stretch="Fill" />
<TextBlock x:Name="viewText"
Grid.Row="1"
Margin="0 5 0 0"
HorizontalAlignment="Center"
FontWeight="Black"
Foreground="Green"
Text="{Binding Title}" />
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=Picture}" Value="{x:Null}">
<Setter TargetName="viewImage" Property="Source" Value="/Images/noImage.png" />
<Setter TargetName="viewText" Property="Text" Value="No Image Available" />
<Setter TargetName="viewText" Property="Foreground" Value="Red" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBlock Grid.Row="0"
Grid.Column="1"
Margin="0 10 0 5"
HorizontalAlignment="Center"
FontSize="16"
FontWeight="Black"
Text="Multi-Data Trigger" />
<ListBox Grid.Row="1"
Grid.Column="1"
Margin="5 0 0 0"
ItemsSource="{Binding ViewItemList}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="0 5 0 0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Image x:Name="viewImage"
Grid.Row="0"
Width="100"
Height="60"
HorizontalAlignment="Center"
Source="{Binding Picture}"
Stretch="Fill" />
<TextBlock x:Name="viewText"
Grid.Row="1"
Margin="0 5 0 0"
HorizontalAlignment="Center"
FontWeight="Black"
Text="{Binding Title}" />
</Grid>
<DataTemplate.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=Picture}" Value="{x:Null}" />
<Condition Binding="{Binding Path=Title}" Value="Waterfall" />
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter TargetName="viewImage" Property="Source" Value="/Images/noImage.png" />
<Setter TargetName="viewImage" Property="Opacity" Value="0.5" />
<Setter TargetName="viewText" Property="Background" Value="Brown" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Grid>
</Window>
标签: wpf
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论