wpf 资源的重用

资源的利用

1) Window.Resource

<Window.Resource>

<ImageBrush x:Key="TitleBrush" TileMode="Tile" Opacity="0.3" ImageSource="happyface.jpg" >

</ImageBrush>

</Window.Resource>

引用

<Button Background="{ StaticResource TileBrush}" >

2) style

<Style x:Key="BigFontButtonStyle">

<Style.Setters>

<Setter Property="Control.FontFamily" Value=" Times Nem Roman" />

<Setter Event="Control.MouseLeave" Handler="element_MouseLeave" />

</Style.Setters>

<Style.Triggers>

//属性值发生变化
<Trigger Property="Control.IsFocused" Value="True" >

<Setter Property="Control.Foreground" Value="Darked" />

</Trigger>

<MultiTrigger>

<MultiTrigger.Conditions>

<Conditon Property="Control.IsFocued" value="True">
<Conditon Property="Control,IsMouseOver" value="True">

</MultiTrigger.Conditions>

<MultiTrigger.Setters>

<Setter Property="Control.Foreground" Value="Darked">

</MultiTrigger.Setters>

</MultiTrigger>

//事件发生变化

<Style.Triggers>

<EventTrigger RouteEvent="Mouse.MouseEnter" >

<EventTrigger.Actions>

<BeginStoryboard>

<Storyboard>

<DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="FontSize"
To="22" />

</Storyboard>

</BeginStoryboard>

<EventTrigger.Actions>

</EventTrigger>

</Style.Triggers>

</Style.Triggers>

</Style>

3)数据模板和控件模板

其一
<ListBox x:Name="ListBox_PeopleList" ItemTemplate="{StaticResource MyTemplate}" />

<Window.Resources>
<!--列表模板-->
<DataTemplate x:Key="MyTemplate" DataType="{x:Type demo:People}">
<Grid VerticalAlignment="Center" HorizontalAlignment="Center"
Margin="4,4,4,4" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Image Source="{Binding Photo}" Width="50" Height="50"
Grid.Column="0" Grid.RowSpan="1"/>
<TextBlock Text="{Binding Name}" Grid.Column="1"
Grid.ColumnSpan="1" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</DataTemplate>
</Window.Resources>

其二
<DataTemplate x:Key="hightlightTmp">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<StackPanel Margin="0,5,0,0" Grid.Row="0" Orientation="Horizontal">
<TextBlock Margin="2,0" FontWeight="Bold" FontSize="14">
<TextBlock.Text>
<Binding Path="Name"
StringFormat="姓名:{0}"/>
</TextBlock.Text>
</TextBlock>
<TextBlock Margin="20,0">
<TextBlock.Text>
<Binding Path="Age"
StringFormat="年龄:{0}"/>
</TextBlock.Text>
</TextBlock>
</StackPanel>
<TextBlock Margin="0,2,0,5" Grid.Row="1">
<TextBlock.Text>
<Binding Path="Email"
StringFormat="电邮:{0}"/>
</TextBlock.Text>
</TextBlock>
</Grid>
</DataTemplate>

<Style x:Key="cmbStyle" TargetType="{x:Type ComboBoxItem}">
<Style.Triggers>
<Trigger Property="IsHighlighted" Value="True">
<Setter Property="ContentTemplate"
Value="{StaticResource hightlightTmp}"/>
</Trigger>
</Style.Triggers>
</Style>

控件模板

<Button Content="看看我的变化" Height="59" HorizontalAlignment="Left" Margin="99,88,0,0" Name="button1"
VerticalAlignment="Top" Width="94" Background="#FFB4AEFC">
<Button.Template>
<ControlTemplate>
<!--定义视觉树-->
<Grid>
<Ellipse Height="{TemplateBinding Control.Height}"
Fill="{TemplateBinding Button.Background}"
Width="{TemplateBinding Button.Width}" />
<TextBlock Name="txtBlock" Margin="{TemplateBinding Button.Padding}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Text="{TemplateBinding Button.Content}"
/>
</Grid>
<!--定义视觉树结束-->
<!--定义触发器-->
<ControlTemplate.Triggers>
<Trigger Property="Button.IsMouseOver" Value="True">
<Setter Property="Button.Foreground" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
<!--定义触发器结束-->
</ControlTemplate>
</Button.Template>
</Button>

上一篇:Prim Algoritm(最小生成树)


下一篇:最好的vim教程莫过于vimtutor