【6月日新计划11】WPF入门-Resource
1. Static Resource/Dynamic Resource
<Window.Resources>
<Style x:Key="btn_left" TargetType="Button">
<Setter Property="Height" Value="35"/>
<Setter Property="Width" Value="100"/>
<Setter Property="Background" Value="AntiqueWhite"/>
<Setter Property="BorderThickness" Value="2"/>
</Style>
<Style x:Key="btn_top" TargetType="Button">
<Setter Property="Height" Value="45"/>
<Setter Property="Width" Value="95"/>
<Setter Property="Background" Value="Chocolate"/>
</Style>
<SolidColorBrush x:Key="Solider" Color="Yellow"/>
</Window.Resources>
<Grid>
<Button Grid.Column="0" Content="btn_top_00" Style="{StaticResource ResourceKey=btn_top}" Click="Change_Color_Click"/>
<Button Grid.Column="1" Content="btn_top_01" Style="{StaticResource ResourceKey=btn_top}" Background="{StaticResource Solider}"/>
<Button Grid.Column="2" Content="btn_top_02" Style="{StaticResource ResourceKey=btn_top}" Background="{DynamicResource Solider}"/>
</Grid>
private void Change_Color_Click(object sender, RoutedEventArgs e)
{
this.Resources["Solider"] = new SolidColorBrush(Colors.Red);
}
2.Resource Dictionary
資源字典就是把一些公共的樣式提取出來。
learn.microsoft.com/en-us/dotne…
1.首先創建資源字典,可參考下面的樣式
2.將樣式導入App.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="myresourcedictionary.xaml"/>
<ResourceDictionary Source="myresourcedictionary2.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
3.獲取資源
App.Current.FindResource("xxx_Key_xxxx")
3. Resource Style
<Window x:Class="resources.ResExample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ResExample" Height="400" Width="300">
<Window.Resources>
<SolidColorBrush x:Key="MyBrush" Color="#05E0E9"/>
<Style TargetType="Border">
<Setter Property="Background" Value="#4E1A3D" />
<Setter Property="BorderThickness" Value="5" />
<Setter Property="BorderBrush">
<Setter.Value>
<LinearGradientBrush>
<GradientStop Offset="0.0" Color="#4E1A3D"/>
<GradientStop Offset="1.0" Color="Salmon"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="TextBlock" x:Key="TitleText">
<Setter Property="FontSize" Value="18"/>
<Setter Property="Foreground" Value="#4E87D4"/>
<Setter Property="FontFamily" Value="Trebuchet MS"/>
<Setter Property="Margin" Value="0,10,10,10"/>
</Style>
<Style TargetType="TextBlock" x:Key="Label">
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="FontSize" Value="13"/>
<Setter Property="Foreground" Value="{StaticResource MyBrush}"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Margin" Value="0,3,10,0"/>
</Style>
</Window.Resources>
<Border>
<StackPanel>
<TextBlock Style="{StaticResource TitleText}">Title</TextBlock>
<TextBlock Style="{StaticResource Label}">Label</TextBlock>
<TextBlock HorizontalAlignment="Right" FontSize="36" Foreground="{StaticResource MyBrush}" Text="Text" Margin="20" />
<Button HorizontalAlignment="Left" Height="30" Background="{StaticResource MyBrush}" Margin="40">Button</Button>
<Ellipse HorizontalAlignment="Center" Width="100" Height="100" Fill="{StaticResource MyBrush}" Margin="10" />
</StackPanel>
</Border>
</Window>
3.1 Itemscontrol
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:UI_Vision">
<Style TargetType="ItemsControl" x:Key="ColorItemsControl">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Label Content="{Binding ItemName}"
Width ="100"
Height="50"
FontSize="35"
Background="Green"></Label>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="ListBox" x:Key="ColorlistBox">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ColorlistBoxItem" TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Label Name="itemName" Content="{Binding ItemName }" Margin="2" Foreground="White" Width="100" Height="40" Background="Green" FontSize="16" FontWeight="Bold"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>