触发器和模板
代码中添加模版
绑定的数据结构

添加处理例程
添加模版的C#代码

如果设置控件的简单属性得不到需要的外观,就可以修改Template属性。使用Template属性可以定制控件的整体外观 。当激活触发器的原因不再有效时,不必将属性值重置为原始值,触发器操作进行的修改会自动重置为原始值。
数据绑定: http://msdn.microsoft.com/zh-cn/library/ms750612(v=vs.110).aspx
监视元素建立触发器的集合仅支持 EventTrigger,而不是属性触发器 ( Trigger)。 如果需要属性触发器,必须将这些是在样式或模板中然后将该样式或模板添加到元素或直接通过 Style 特性,或者通过隐式样式请取消引用。
注意在 DataTrigger 必须指定 Binding 和 Value 属性以数据触发器可以是有意义的。 如果一个或两个未指定属性,将引发异常。
WPFtrigger的主要类型有:Trigger、MultiTrigger、DataTrigger、MultiDataTrigger、EventTrigger几种。trigger主要运用的场景在Style、ControlTemplate、DataTemplate三个地方。在这些地方可以使用trigger,具体视情况而定。
若想在与EventTrigger类似的控件下的位置定义Trigger及DataTrigger可以采用例下方式:
例: <TextBox Canvas.Left="146" Canvas.Top="199" Height="23" Name="textBox2" Width="120" Margin="62,47,321,241"> <TextBox.Resources> <Style TargetType="{x:Type TextBox}"> <Style.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self},Path=Text,Converter={StaticResource dc}}" Value="True"> <Setter Property="Foreground" Value="Red" ></Setter> </DataTrigger> </Style.Triggers> </Style> </TextBox.Resources> </TextBox>
1.属性触发器 (条件与条件之间属于 or)
2,多属性触发器 (条件与条件之间属于 and)
3,数据触发器 (条件与条件之间属于 or)
模板
TargetNullValue与FallbackValue都是BindingBase的属性。 TargetNullValue:获取或设置当源的值为 nullNothingnullptrunitnull 引用(在 Visual Basic 中为 Nothing) 时在目标中使用的值。 FallbackValue:获取或设置当绑定无法返回值或无法解析源路径时要使用的值。 下面代码中,我们在TextBlock中绑定一个Coutry值 <TextBlock x:Name="CountryValueTextBlock" Grid.Row="8" Grid.Column="1" Margin="2" Text="{Binding Country, TargetNullValue=CountryNull, FallbackValue=CountryFallback}"> </TextBlock>
这儿我们给 TargetNullValue与FallbackValue都设置了一个值。
后台代码中,我们将Country的值绑定代码注释掉
//region.Country = "China";
Country显示 TargetNullValue属性设置好的默认值:
![]()
如果我们把绑定值名称修改成一个不存在的名称
Text="{Binding Country111, TargetNullValue=CountryNull, FallbackValue=CountryFallback}"
Country显示 FallbackValue属性设置好的默认值:
1,控件模板 控件的模板属性(ControlTemplate ContentPresenter)
2,数据模板 (定义控件)
3, 列表元素控件模板使用Grid定义列表,ItemTemplate定义一了个Item的显示方式, Template定义了控件的Item元素显示在什么地方!
对于列表框,ControlTemplate定 义了 整个控件的外观,ItemTemplate定 义了列表项的外观,
DataTemplate定 义了 项中的 类型。
1 <Application x:Class="WpfApplication1.App" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 StartupUri="MainWindow.xaml"> 5 <Application.Resources> 6 <ResourceDictionary> 7 <ResourceDictionary.MergedDictionaries> 8 <ResourceDictionary Source="Dictionary1.xaml"/> 9 </ResourceDictionary.MergedDictionaries> 10 </ResourceDictionary> 11 </Application.Resources> 12 </Application>
1 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 2 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 3 <!--<LinearGradientBrush x:Key="CyanGradientBrush" StartPoint="0,0" EndPoint="0.3,1"> 4 <GradientStop Offset="0.0" Color="LightCyan"/> 5 <GradientStop Offset="0.14" Color="Cyan"/> 6 <GradientStop Offset="0.7" Color="DarkCyan"/> 7 </LinearGradientBrush> 8 9 <Style x:Key="PinkButtonStyle" TargetType="Button"> 10 <Setter Property="FontSize" Value="18"/> 11 <Setter Property="Foreground" Value="White"/> 12 <Setter Property="Background"> 13 <Setter.Value> 14 <LinearGradientBrush StartPoint="0,0" EndPoint="0,1.0"> 15 <GradientStop Offset="0.0" Color="Pink"/> 16 <GradientStop Offset="0.3" Color="DeepPink"/> 17 <GradientStop Offset="1.0" Color="DarkOrchid"/> 18 </LinearGradientBrush> 19 </Setter.Value> 20 </Setter> 21 </Style> 22 <Style x:Key="RoundedGelButton" TargetType="Button"> //******************************控件模板 23 <Setter Property="Width" Value="100"/> 24 <Setter Property="Height" Value="100"/> 25 <Setter Property="Foreground" Value="White"/> 26 <Setter Property="Template"> 27 <Setter.Value> 28 <ControlTemplate TargetType="{x:Type Button}"> 29 <ControlTemplate.Triggers> 30 <Trigger Property="IsMouseOver" Value="True"> 31 <Setter Property="Ellipse.Fill" TargetName="GelBackground"> 32 <Setter.Value> 33 <RadialGradientBrush> 34 <GradientStop Offset="0" Color="Lime"/> 35 <GradientStop Offset="1" Color="DarkGreen"/> 36 </RadialGradientBrush> 37 </Setter.Value> 38 </Setter> 39 </Trigger> 40 </ControlTemplate.Triggers> 41 <Grid> 42 <Ellipse Name="GelBackground" StrokeThickness="0.5" Fill="Black"> 43 <Ellipse.Stroke> 44 <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> 45 <GradientStop Offset="0" Color="#ff7e7e7e"/> 46 <GradientStop Offset="1" Color="Black"/> 47 </LinearGradientBrush> 48 </Ellipse.Stroke> 49 </Ellipse> 50 <Ellipse Margin="15,5,15,50"> 51 <Ellipse.Fill> 52 <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> 53 <GradientStop Offset="0" Color="#aaffffff"/> 54 <GradientStop Offset="1" Color="Transparent"/> 55 </LinearGradientBrush> 56 </Ellipse.Fill> 57 </Ellipse> 58 <ContentPresenter Name="GelButtonContent" 59 VerticalAlignment="Center" 60 HorizontalAlignment="Center" 61 Content="dddddddddddddd"/> 62 </Grid> 63 </ControlTemplate> 64 </Setter.Value> 65 </Setter> 66 67 </Style>--> 68 </ResourceDictionary>
1 <Window x:Class="WpfApplication1.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="MainWindow" Height="350" Width="525"> 5 <Window.Resources> 6 <!--<Style TargetType="Button"> //***********************属性触发器 7 <Setter Property="Background" Value="LightBlue"/> 8 <Setter Property="FontSize" Value="17"/> 9 <Style.Triggers> 10 <Trigger Property="IsMouseOver" Value="True"> 11 <Setter Property="Foreground" Value="Red"/> 12 <Setter Property="FontSize" Value="22"/> 13 </Trigger> 14 <Trigger Property="IsPressed" Value="True"> 15 <Setter Property="Foreground" Value="Yellow"/> 16 <Setter Property="FontSize" Value="22"/> 17 </Trigger> 18 </Style.Triggers> 19 </Style>--> 20 <!--<Style TargetType="TextBox"> //***********************多触发器 21 <Style.Triggers> 22 <MultiTrigger> 23 <MultiTrigger.Conditions> 24 <Condition Property="IsEnabled" Value="True"/> 25 <Condition Property="Text" Value="Test"/> 26 </MultiTrigger.Conditions> 27 <MultiTrigger.Setters> 28 <Setter Property="Foreground" Value="Red"/> 29 </MultiTrigger.Setters> 30 </MultiTrigger> 31 </Style.Triggers> 32 </Style> 33 <Style TargetType="ListBoxItem"> //************************数据触发器 34 <Style.Triggers> 35 <DataTrigger Binding="{Binding Path=Publisher}" Value="Wrox Press"> 36 <Setter Property="Background" Value="Red"/> 37 </DataTrigger> 38 <DataTrigger Binding="{Binding Path=Publisher}" Value="Dummies"> 39 <Setter Property="Background" Value="Yellow"/> 40 </DataTrigger> 41 <DataTrigger Binding="{Binding Path=Publisher}" Value="Sybex"> 42 <Setter Property="Background" Value="LightBlue"/> 43 </DataTrigger> 44 <MultiDataTrigger> 45 <MultiDataTrigger.Conditions> 46 <Condition Binding="{Binding Path=Publisher}" Value="Dummies"/> 47 </MultiDataTrigger.Conditions> 48 <MultiDataTrigger.Setters> 49 <Setter Property="Background" Value="Yellow"/> 50 </MultiDataTrigger.Setters> 51 </MultiDataTrigger> 52 </Style.Triggers> 53 </Style>--> 54 <!--<DataTemplate x:Key="datatemplate"> //*********************数据模板 55 <Grid> 56 <Grid.ColumnDefinitions> 57 <ColumnDefinition Width="Auto"/> 58 <ColumnDefinition Width="Auto"/> 59 </Grid.ColumnDefinitions> 60 <Grid.RowDefinitions> 61 <RowDefinition Height="60"/> 62 </Grid.RowDefinitions> 63 <TextBox FontSize="16" VerticalAlignment="Top" Margin="5" 64 Text="{Binding Name}" FontWeight="Bold" Grid.Column="0"/> 65 <Border Margin="4.0" Grid.Column="1" BorderThickness="2" 66 CornerRadius="4"> 67 <Border.BorderBrush> 68 <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> 69 <GradientStop Offset="0" Color="#222"/> 70 <GradientStop Offset="1" Color="#222"/> 71 </LinearGradientBrush> 72 </Border.BorderBrush> 73 <Grid> 74 <Rectangle> 75 <Rectangle.Fill> 76 <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> 77 <GradientStop Offset="0" Color="#444"/> 78 <GradientStop Offset="1" Color="#444"/> 79 </LinearGradientBrush> 80 </Rectangle.Fill> 81 </Rectangle> 82 <Image Width="48" Margin="2,2,2,1" 83 Source="{Binding ImagePath}"/> 84 </Grid> 85 </Border> 86 </Grid> 87 </DataTemplate>--> 88 <Style x:Key="ListBoxStyle1" TargetType="{x:Type ListBox}"> //*********************样式化列表1 89 <Setter Property="ItemTemplate"> 90 <Setter.Value> 91 <DataTemplate> 92 <Grid> 93 <Grid.ColumnDefinitions> 94 <ColumnDefinition Width="Auto"/> 95 <ColumnDefinition Width="*" SharedSizeGroup="MiddleColumn"/> 96 <ColumnDefinition Width="Auto"/> 97 </Grid.ColumnDefinitions> 98 <Grid.RowDefinitions> 99 <RowDefinition Height="60"/> 100 </Grid.RowDefinitions> 101 <TextBlock FontSize="16" VerticalAlignment="Center" Margin="5" 102 FontStyle="Italic" Grid.Column="0" Text="Country:"/> 103 <TextBlock FontSize="16" VerticalAlignment="Center" Margin="5" 104 FontWeight="Bold" Grid.Column="1" Text="{Binding Name}"/> 105 <Border Margin="4,0" Grid.Column="2" BorderThickness="2" CornerRadius="4"> 106 <Border.BorderBrush> 107 <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> 108 <GradientStop Offset="0" Color="#aaa"/> 109 <GradientStop Offset="1" Color="#222"/> 110 </LinearGradientBrush> 111 </Border.BorderBrush> 112 <Grid> 113 <Rectangle> 114 <Rectangle.Fill> 115 <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> 116 <GradientStop Offset="0" Color="#444"/> 117 <GradientStop Offset="1" Color="#fff"/> 118 </LinearGradientBrush> 119 </Rectangle.Fill> 120 </Rectangle> 121 <Image Width="48" Margin="2,2,2,1" 122 Source="{Binding ImagePath}"/> 123 </Grid> 124 </Border> 125 </Grid> 126 </DataTemplate> 127 </Setter.Value> 128 </Setter> 129 <Setter Property="Grid.IsSharedSizeScope" Value="True"/> 130 </Style> 131 <Style x:Key="ListBoxStyle2" TargetType="{x:Type ListBox}" 132 BasedOn="{StaticResource ListBoxStyle1}"> //********************样式化列表2 133 <Setter Property="Template"> 134 <Setter.Value> 135 <ControlTemplate TargetType="{x:Type ListBox}"> 136 <ScrollViewer HorizontalScrollBarVisibility="Auto"> 137 <StackPanel Name="StackPanel1" IsItemsHost="True" 138 Orientation="Horizontal"/> 139 </ScrollViewer> 140 </ControlTemplate> 141 </Setter.Value> 142 </Setter> 143 <Setter Property="VerticalAlignment" Value="Center"/> 144 </Style> 145 <Style x:Key="ListBoxStyle3" TargetType="{x:Type ListBox}"> 146 <Setter Property="Template"> 147 <Setter.Value> 148 <ControlTemplate TargetType="{x:Type ListBox}"> 149 <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled"> 150 <WrapPanel Name="StackPanel1" IsItemsHost="True"/> 151 </ScrollViewer> 152 </ControlTemplate> 153 </Setter.Value> 154 </Setter> 155 <Setter Property="ItemTemplate"> 156 <Setter.Value> 157 <DataTemplate> 158 <Grid> 159 <Grid.ColumnDefinitions> 160 <ColumnDefinition Width="140"/> 161 </Grid.ColumnDefinitions> 162 <Grid.RowDefinitions> 163 <RowDefinition Height="60"/> 164 <RowDefinition Height="30"/> 165 </Grid.RowDefinitions> 166 <Image Grid.Row="0" Width="48" Margin="2,2,2,1" 167 Source="{Binding ImagePath}" /> 168 <TextBlock Grid.Row="1" FontSize="14" 169 HorizontalAlignment="Center" Margin="5" 170 Text="{Binding Name}"/> 171 </Grid> 172 </DataTemplate> 173 </Setter.Value> 174 </Setter> 175 <Setter Property="VerticalAlignment" Value="Center"/> 176 </Style> 177 </Window.Resources> 178 <Grid Height="300"> 179 <!--<Button Grid.Row="1" x:Name="button1" Margin="10" ContentTemplate="{StaticResource datatemplate}"/>--> 180 <ListBox Name="listbox111" Style="{StaticResource ListBoxStyle3}" ItemsSource="{Binding}" Margin="10"/> 181 <!--<StackPanel x:Name="myContainer"> 182 <StackPanel.Resources> 183 <LinearGradientBrush x:Key="myGradientBrush" StartPoint="0,0" EndPoint="0.3,1"> 184 <GradientStop Offset="0.0" Color="LightCoral"/> 185 <GradientStop Offset="0.14" Color="Cyan"/> 186 <GradientStop Offset="0.7" Color="DarkCyan"/> 187 </LinearGradientBrush> 188 </StackPanel.Resources> 189 <Button Width="200" Height="50" Margin="5" Style="{StaticResource RoundedGelButton}" Content="Click Me" Click="Button_Click"> 190 </Button> 191 <TextBox /> 192 <ListBox x:Name="list1"/> 193 </StackPanel>--> 194 </Grid> 195 </Window>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApplication1 { public class Book { public string Title { get; set; } public string Publisher{get;set;} public override string ToString() { return Title; } } public class Country { public string Name { get; set; } public string ImagePath { get; set; } public override string ToString() { return Name; } } public class Countries { public static IEnumerable<Country> GetCountries() { return new List<Country> { new Country { Name = "Austria", ImagePath = "111.bmp"}, new Country { Name = "Germany", ImagePath = "111.bmp"}, new Country { Name = "Norway", ImagePath = "111.bmp"}, new Country { Name = " USA", ImagePath = "111.bmp"} }; } } /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); //button1.Content = new Country //{ // Name = "Austria", // ImagePath = "111.bmp" //}; listbox111.DataContext= Countries.GetCountries(); //list1.Items.Add(new Book //{ // Title = "Professional C# 4.0", // Publisher = "Wrox Press" //}); //list1.Items.Add(new Book //{ // Title = "Professional C# 4.0", // Publisher = "Dummies" //}); //list1.Items.Add(new Book //{ // Title = "Professional C# 4.0", // Publisher = "Sybex" //}); } private void Button_Click(object sender, RoutedEventArgs e) { //myContainer.Resources.Clear(); //var brush = new LinearGradientBrush //{ // StartPoint = new Point(0,0), // EndPoint = new Point(0,1) //}; //brush.GradientStops = new GradientStopCollection() //{ // new GradientStop(Colors.White, 0.0), // new GradientStop(Colors.Yellow, 0.14), // new GradientStop(Colors.YellowGreen, 0.7) //}; //myContainer.Resources.Add("myGradientBrush", brush); } } }

浙公网安备 33010602011771号