"Consistency in a user interface is an important trait; there are many facets of consistency,  
one of which is the consistent look and feel of controls. For example, all buttons should  
look roughly the same – similar colors, the same margins, and so on."

UI的风格一致性是应用程序应当关注的重要特性。

“Styles provide a convenient way to group a set of properties (and triggers) under a single
object, and then selectively (or automatically as we'll see later) apply it to elements.”

1.Creating and using styles

用一个Demo,来总结Style。

MainWindow.xaml如下:

  1. <Window x:Class="CreatingAndUsingStyle.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="Styled Calculatot" Height="269" Width="180" ResizeMode="CanMinimize">
  5. <Window.Resources>
  6. <Style TargetType="Button" x:Key="numericStyle">
  7. <Setter Property="FontSize" Value="20" />
  8. <Setter Property="Margin" Value="4" />
  9. <Setter Property="Padding" Value="6" />
  10. <Setter Property="Effect">
  11. <Setter.Value>
  12. <DropShadowEffect Color="Blue"/>
  13. </Setter.Value>
  14. </Setter>
  15. </Style>
  16.  
  17. <Style TargetType="Button" x:Key="operatorStyle" BasedOn="{StaticResource numericStyle}">
  18. <Setter Property="FontWeight" Value="ExtraBold" />
  19. <Setter Property="Effect">
  20. <Setter.Value>
  21. <DropShadowEffect Color="Red" />
  22. </Setter.Value>
  23. </Setter>
  24. </Style>
  25. </Window.Resources>
  26.  
  27. <Grid>
  28. <Grid.ColumnDefinitions>
  29. <ColumnDefinition Width="Auto" />
  30. <ColumnDefinition Width="Auto" />
  31. <ColumnDefinition Width="Auto" />
  32. <ColumnDefinition Width="Auto" />
  33. </Grid.ColumnDefinitions>
  34. <Grid.RowDefinitions>
  35. <RowDefinition Height="Auto" />
  36. <RowDefinition Height="Auto" />
  37. <RowDefinition Height="Auto" />
  38. <RowDefinition Height="Auto" />
  39. <RowDefinition Height="Auto" />
  40. </Grid.RowDefinitions>
  41. <TextBox Background="Cyan" IsReadOnly="True" Grid.ColumnSpan="4"/>
  42. <Button Content="7" Grid.Row="1" Style="{StaticResource numericStyle}"/>
  43. <Button Content="8" Grid.Row="1" Grid.Column="1" Style="{StaticResource numericStyle}"/>
  44. <Button Content="9" Grid.Row="1" Grid.Column="2" Style="{StaticResource numericStyle}"/>
  45. <Button Content="4" Grid.Row="2" Style="{StaticResource numericStyle}"/>
  46. <Button Content="5" Grid.Row="2" Grid.Column="1" Style="{StaticResource numericStyle}"/>
  47. <Button Content="6" Grid.Row="2" Grid.Column="2" Style="{StaticResource numericStyle}"/>
  48. <Button Content="1" Grid.Row="3" Style="{StaticResource numericStyle}"/>
  49. <Button Content="2" Grid.Row="3" Grid.Column="1" Style="{StaticResource numericStyle}"/>
  50. <Button Content="3" Grid.Row="3" Grid.Column="2" Style="{StaticResource numericStyle}"/>
  51. <Button Content="0" Grid.Row="4" Style="{StaticResource numericStyle}"/>
  52. <Button Content="=" Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2" Style="{StaticResource operatorStyle}">
  53. <Button.Effect>
  54. <DropShadowEffect Color="Green" />
  55. </Button.Effect>
  56. </Button>
  57. <Button Content="+" Grid.Row="4" Grid.Column="3" Style="{StaticResource operatorStyle}"/>
  58. <Button Content="-" Grid.Row="3" Grid.Column="3" Style="{StaticResource operatorStyle}"/>
  59. <Button Content="X" Grid.Row="2" Grid.Column="3" Style="{StaticResource operatorStyle}"/>
  60. <Button Content="/" Grid.Row="1" Grid.Column="3" Style="{StaticResource operatorStyle}"/>
  61. </Grid>
  62. </Window>

效果如下:

Point of Interest:


1.“A  Style is a container for a bunch of  Setter objects (and triggers, as we'll see later). Each
setter indicates which property should have which value; the property must be a dependency
property. The  FrameworkElement class exposes a Style property that can be set to such a
Style object. Styles are always built as resources.”

Setter的Property必须是依赖属性;

FrameworkStyle暴露了Style这个属性;

Style一般放在Resources中.


2."The  TargetType property of a  Style is typically set, which makes the  Style applicable to
that particular type (this can be any type, even a type of a custom control) and any derived
types."

TargetType="Button" 指定Style应用的类型。

一般,我们使用Style的时候,我们都会设置这个(In practice ,TargetType is always specified.),想想为什么?


3. x:Key="numericStyle" 可以根据需要进行有或无。

a.当其有的时候,我们需要对需要 Style="{StaticResource numericStyle}" .

b.其不设置的时候,默认所有TargetType使用此Style。  见2.


4. BasedOn="{StaticResource numericStyle}" 这是Style的继承。

Style继承时,可以修改BaseOn Style的Setter。如operatorStyle的          部分所示。

Style继承很好用,但是要注意:基Style的修改会影响子style.


5.“An element that uses a style can change a property that is set explicitly by a Style (a local
value), and this is stronger than a Style property setter.”

对于应用了Style的element,我们可以设置其属性值,且这个优先级更高(覆盖Style设置)。如"="Button的         部分所示。

2.Applying a style automatically

我们移除 x:Key="numericStyle"。让所有Button使用这个style.

MainWindow.xaml修改如下:

  1. <Window x:Class="CreatingAndUsingStyle.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="Styled Calculatot" Height="269" Width="180" ResizeMode="CanMinimize">
  5. <Window.Resources>
  6. <Style TargetType="Button">
  7. <Setter Property="FontSize" Value="20" />
  8. <Setter Property="Margin" Value="4" />
  9. <Setter Property="Padding" Value="6" />
  10. <Setter Property="Effect">
  11. <Setter.Value>
  12. <DropShadowEffect Color="Blue"/>
  13. </Setter.Value>
  14. </Setter>
  15. </Style>
  16.  
  17. <Style TargetType="Button" x:Key="operatorStyle" BasedOn="{StaticResource {x:Type Button}}">
  18. <Setter Property="FontWeight" Value="ExtraBold" />
  19. <Setter Property="Effect">
  20. <Setter.Value>
  21. <DropShadowEffect Color="Red" />
  22. </Setter.Value>
  23. </Setter>
  24. </Style>
  25. </Window.Resources>
  26.  
  27. <Grid>
  28. <Grid.ColumnDefinitions>
  29. <ColumnDefinition Width="Auto" />
  30. <ColumnDefinition Width="Auto" />
  31. <ColumnDefinition Width="Auto" />
  32. <ColumnDefinition Width="Auto" />
  33. </Grid.ColumnDefinitions>
  34. <Grid.RowDefinitions>
  35. <RowDefinition Height="Auto" />
  36. <RowDefinition Height="Auto" />
  37. <RowDefinition Height="Auto" />
  38. <RowDefinition Height="Auto" />
  39. <RowDefinition Height="Auto" />
  40. </Grid.RowDefinitions>
  41. <TextBox Background="Cyan" IsReadOnly="True" Grid.ColumnSpan="4"/>
  42. <Button Content="7" Grid.Row="1" />
  43. <Button Content="8" Grid.Row="1" Grid.Column="1" />
  44. <Button Content="9" Grid.Row="1" Grid.Column="2" />
  45. <Button Content="4" Grid.Row="2" />
  46. <Button Content="5" Grid.Row="2" Grid.Column="1" />
  47. <Button Content="6" Grid.Row="2" Grid.Column="2" />
  48. <Button Content="1" Grid.Row="3" />
  49. <Button Content="2" Grid.Row="3" Grid.Column="1" />
  50. <Button Content="3" Grid.Row="3" Grid.Column="2" />
  51. <Button Content="0" Grid.Row="4" Style="{x:Null}"/>
  52. <Button Content="=" Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2" Style="{StaticResource operatorStyle}">
  53. <Button.Effect>
  54. <DropShadowEffect Color="Green" />
  55. </Button.Effect>
  56. </Button>
  57. <Button Content="+" Grid.Row="4" Grid.Column="3" Style="{StaticResource operatorStyle}"/>
  58. <Button Content="-" Grid.Row="3" Grid.Column="3" Style="{StaticResource operatorStyle}"/>
  59. <Button Content="X" Grid.Row="2" Grid.Column="3" Style="{StaticResource operatorStyle}"/>
  60. <Button Content="/" Grid.Row="1" Grid.Column="3" Style="{StaticResource operatorStyle}"/>
  61. </Grid>
  62. </Window>

效果如下:

Point of Interest

1.“Automatic styles are created as resources without a key. This does not mean there is no key,
because it's still a dictionary. The key becomes the actual type to apply the style to defined by
the  TargetType property.”

不为Style指定x:key,这个Style将应用于所有的x:Tyle element;

没有指定x:key,不代表其没有key,其key由x:Tyle定义,就本例而言是{x:Type Button}},从Style继承中(xaml中绿色高亮代码),可见一斑。


2.如果,这样的Style被Set在Window的Resource,则影响这个Window上的所有x:Type类型的element;

如果被set在application's resources,则影响所有window。


3.“The Style is applied to all elements of the target type, but not derived types.
Any element that does not set its style explicitly obtains that style automatically. If an element
wishes to revert to its default style, it can set its Style property to null ( {x:Null}  in XAML)
or set its  Style to another named style.”

如果,我们希望某个element不应用这个style,我们可以设置它的Style属性为其他style;

如果想让它保持默认的样子,我们可以设置其Style="{x:Null}",如Button “0”。

3.动态更换Style

“Automatic styles are a great way to create a consistent look and feel without burdening the
developer (or the designer) with the details of the various visual properties. It can also be
used as a quick way to implement skinning."

在Skin文件夹中添加两个Resource Dictionary,如下:

  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  3. <Style TargetType="TextBox">
  4. <Setter Property="Background" Value="LightBlue" />
  5. <Setter Property="Foreground" Value="Black" />
  6. <Setter Property="BorderThickness" Value="2" />
  7. <Setter Property="BorderBrush" Value="DarkBlue" />
  8. <Style.Triggers>
  9. <Trigger Property="IsReadOnly" Value="True">
  10. <Setter Property="Background" Value="Blue" />
  11. </Trigger>
  12. </Style.Triggers>
  13. </Style>
  14. <Style TargetType="Button">
  15. <Setter Property="Background" Value="Cyan" />
  16. </Style>
  17. </ResourceDictionary>
  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  3. <Style TargetType="TextBox">
  4. <Setter Property="Background" Value="Violet" />
  5. <Setter Property="Foreground" Value="DarkViolet" />
  6. <Setter Property="BorderThickness" Value="2" />
  7. <Setter Property="BorderBrush" Value="LightPink" />
  8.  
  9. <Style.Triggers>
  10. <Trigger Property="IsReadOnly" Value="True">
  11. <Setter Property="Background" Value="Pink" />
  12. </Trigger>
  13. </Style.Triggers>
  14. </Style>
  15. <Style TargetType="Button">
  16. <Setter Property="Background" Value="Red" />
  17. </Style>
  18. </ResourceDictionary>

并设置其Property为:Content/Aways Copy.

“These settings prevent the default compilation to BAML and also provide the flexibility to
change the skins without recompilation. ”

请参考DebugLZQ前面的博文:WPF整理-二进制资源和内容

MainWindow.xaml,MainWindow.xaml.cs如下:

  1. <Window x:Class="WPFStyleTriggerAndControlTemplate.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="303.719" Width="530.785">
  5. <Grid Margin="0,0,2,0">
  6. <TextBox HorizontalAlignment="Left" Height="23" Margin="10,35,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
  7. <TextBox HorizontalAlignment="Left" Height="23" Margin="156,35,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
  8. <TextBox HorizontalAlignment="Left" Height="23" Margin="293,35,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" RenderTransformOrigin="1.577,0.303"/>
  9. <Button Content="Button" HorizontalAlignment="Left" Margin="433,35,0,0" VerticalAlignment="Top" Width="75"/>
  10. <GroupBox FontSize="28" Header="Select Skin" Margin="75,102,62,0" VerticalAlignment="Top" Height="158">
  11. <StackPanel Orientation="Vertical" HorizontalAlignment="Center">
  12. <RadioButton Content="Normal" Checked="AllRadioButtonChecked"/>
  13. <RadioButton Content="Blue Skin" Checked="AllRadioButtonChecked"/>
  14. <RadioButton Content="Red Skin" Checked="AllRadioButtonChecked"/>
  15. </StackPanel>
  16. </GroupBox>
  17. </Grid>
  18. </Window>
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Markup;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16.  
  17. namespace WPFStyleTriggerAndControlTemplate
  18. {
  19. /// <summary>
  20. /// Interaction logic for MainWindow.xaml
  21. /// </summary>
  22. public partial class MainWindow : Window
  23. {
  24. public MainWindow()
  25. {
  26. InitializeComponent();
  27. }
  28.  
  29. private void AllRadioButtonChecked(object sender, RoutedEventArgs e)
  30. {
  31. switch (((RadioButton)sender).Content.ToString())
  32. {
  33. case "Blue Skin":
  34. ChangeSkin("Skins/BlueSkin.xaml");
  35. break;
  36. case "Red Skin":
  37. ChangeSkin("Skins/RedSkin.xaml");
  38. break;
  39. case "Normal":
  40. Application.Current.Resources.MergedDictionaries.Clear();
  41. break;
  42. }
  43. }
  44.  
  45. void ChangeSkin(string skinRelativeUri)
  46. {
  47. var si = Application.GetContentStream(new Uri(skinRelativeUri, UriKind.Relative));
  48. var rd = (ResourceDictionary)XamlReader.Load(si.Stream);
  49. Application.Current.Resources.MergedDictionaries.Clear();
  50. Application.Current.Resources.MergedDictionaries.Add(rd);
  51. }
  52.  
  53. }
  54. }

运行效果如下:

4.Other places to set styles

"The example shows styles applied to the FrameworkElement.Style property, but this is
not the only property that can accept a Style.

Other examples include the following:"

  "FrameworkElement.FocusVisualStyle property: Accepts a Style that affects
  the way the focus indicator is rendered when that element has the keyboard focus."

  "ItemsControl.ItemContainerStyle property: Accepts a Style that affects the
  container element of each data item (for example, ListBoxItem for a ListBox)."

  "DataGrid.CellStyle: Accepts a Style that affects the way a cell is rendered.
  Similar properties exposed by the DataGrid include ColumnHeaderStyle,
  DragIndicatorStyle, DropLocationIndicatorStyle, RowHeaderStyle,
  and RowStyle."

5.Style和ControlTemplate

ControlTemplate一般有如下2种使用方法

方法1:

<Window.Resources>
        <ControlTemplate x:Key="AnimatedExpanderButtonTemp" TargetType="{x:Type ToggleButton}">

Template="{StaticResource RevealExpanderTemp}"

--------------

方法2:

<Style  TargetType="{x:Type TabControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabControl}">
                        <Grid>

-----------------------------

关于DataTemplate请参考DebugLZQ前面的博文:WPF整理-使用 DataTemplate

6.Style & Trigger & ControlTemplate一个非常常见的例子

在实际应用中,这3个一般会同时用到。我们用一个自定义的最大化/最小化/关闭按钮等,如下:

一般有2种实现方法,第一种方法如下:

  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="362.767" Width="524.468" WindowStyle="None" AllowsTransparency="True" Background="Transparent">
  5. <Window.Resources>
  6. <Style TargetType="Button" x:Key="btnStyle1">
  7. <Setter Property="Template">
  8. <Setter.Value>
  9. <ControlTemplate TargetType="Button">
  10. <Grid x:Name="grid1" Background="Transparent">
  11. <ContentPresenter Content="{TemplateBinding Content}" VerticalAlignment="Center" HorizontalAlignment="Center" RecognizesAccessKey="True"/>
  12. </Grid>
  13. <ControlTemplate.Triggers>
  14. <Trigger Property="IsMouseOver" Value="True">
  15. <Setter TargetName="grid1" Property="Grid.Background" Value="Red"/>
  16. </Trigger>
  17. </ControlTemplate.Triggers>
  18. </ControlTemplate>
  19. </Setter.Value>
  20. </Setter>
  21. </Style>
  22.  
  23. <ControlTemplate TargetType="Button" x:Key="btnTemplate1">
  24. <Grid x:Name="grid1" Background="Transparent" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
  25. <ContentPresenter Content="{TemplateBinding Content}" VerticalAlignment="Center" HorizontalAlignment="Center" RecognizesAccessKey="True"/>
  26. </Grid>
  27. <ControlTemplate.Triggers>
  28. <Trigger Property="IsMouseOver" Value="True">
  29. <Setter TargetName="grid1" Property="Grid.Background" Value="LightBlue"/>
  30. </Trigger>
  31. </ControlTemplate.Triggers>
  32. </ControlTemplate>
  33. </Window.Resources>
  34. <Grid >
  35. <Grid.Background>
  36. <ImageBrush ImageSource="Button_Icons/background_mainwnd.png"/>
  37. </Grid.Background>
  38. <Grid.RowDefinitions>
  39. <RowDefinition Height="Auto"/>
  40. <RowDefinition Height="*"/>
  41. </Grid.RowDefinitions>
  42. <StackPanel Grid.Row="0" Orientation="Horizontal" FlowDirection="RightToLeft">
  43. <Button Style="{StaticResource btnStyle1}" Content="X" Foreground="White" FontSize="20" HorizontalAlignment="Left" VerticalAlignment="Top" Width="23" Height="23" Margin="5,0,5,0"/>
  44. <Button Template="{StaticResource btnTemplate1}" Content="口" Foreground="White" FontSize="20" HorizontalAlignment="Left" VerticalAlignment="Top" Width="23" Height="23" Margin="5,0,5,0"/>
  45. <Button Template="{StaticResource btnTemplate1}" Content="一" Foreground="White" FontSize="20" HorizontalAlignment="Left" VerticalAlignment="Top" Width="23" Height="23" Margin="5,0,5,0"/>
  46.  
  47. </StackPanel>
  48. </Grid>
  49. </Window>

效果如下:

这种方法的缺点是,我们设置的Content比较粗糙~

第二中方法,借助相关的图标的png图片及相应的效果图片,来设置Content,原理同上

  1. <Window x:Class="WpfApplication1.Window1"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="Window1" Height="362.767" Width="524.468" WindowStyle="None" AllowsTransparency="True" Background="Transparent">
  5. <Window.Resources>
  6. <Style TargetType="Button" x:Key="btnStyle1">
  7. <Setter Property="Template">
  8. <Setter.Value>
  9. <ControlTemplate TargetType="Button">
  10. <Grid Background="Transparent">
  11. <ContentPresenter x:Name="cp1" Content="{TemplateBinding Content}" VerticalAlignment="Center" HorizontalAlignment="Center" RecognizesAccessKey="True"/>
  12. </Grid>
  13. <ControlTemplate.Triggers>
  14. <Trigger Property="IsMouseOver" Value="True">
  15. <Setter TargetName="cp1" Property="Content">
  16. <Setter.Value>
  17. <Image Source="Button_Icons/xm.png"/>
  18. </Setter.Value>
  19. </Setter>
  20. </Trigger>
  21. </ControlTemplate.Triggers>
  22. </ControlTemplate>
  23. </Setter.Value>
  24. </Setter>
  25. </Style>
  26. <Style TargetType="Button" x:Key="btnStyle2">
  27. <Setter Property="Template">
  28. <Setter.Value>
  29. <ControlTemplate TargetType="Button">
  30. <Grid Background="Transparent">
  31. <ContentPresenter x:Name="cp1" Content="{TemplateBinding Content}" VerticalAlignment="Center" HorizontalAlignment="Center" RecognizesAccessKey="True"/>
  32. </Grid>
  33. <ControlTemplate.Triggers>
  34. <Trigger Property="IsMouseOver" Value="True">
  35. <Setter TargetName="cp1" Property="Content">
  36. <Setter.Value>
  37. <Image Source="Button_Icons/mxm.png"/>
  38. </Setter.Value>
  39. </Setter>
  40. </Trigger>
  41. </ControlTemplate.Triggers>
  42. </ControlTemplate>
  43. </Setter.Value>
  44. </Setter>
  45. </Style>
  46. <Style TargetType="Button" x:Key="btnStyle3">
  47. <Setter Property="Template">
  48. <Setter.Value>
  49. <ControlTemplate TargetType="Button">
  50. <Grid Background="Transparent">
  51. <ContentPresenter x:Name="cp1" Content="{TemplateBinding Content}" VerticalAlignment="Center" HorizontalAlignment="Center" RecognizesAccessKey="True"/>
  52. </Grid>
  53. <ControlTemplate.Triggers>
  54. <Trigger Property="IsMouseOver" Value="True">
  55. <Setter TargetName="cp1" Property="Content">
  56. <Setter.Value>
  57. <Image Source="Button_Icons/mim.png"/>
  58. </Setter.Value>
  59. </Setter>
  60. </Trigger>
  61. </ControlTemplate.Triggers>
  62. </ControlTemplate>
  63. </Setter.Value>
  64. </Setter>
  65. </Style>
  66. </Window.Resources>
  67.  
  68. <Grid >
  69. <Grid.Background>
  70. <ImageBrush ImageSource="Button_Icons/background_mainwnd.png"/>
  71. </Grid.Background>
  72. <Grid.RowDefinitions>
  73. <RowDefinition Height="Auto"/>
  74. <RowDefinition Height="*"/>
  75. </Grid.RowDefinitions>
  76. <StackPanel Grid.Row="0" Orientation="Horizontal" FlowDirection="RightToLeft">
  77. <Button Style="{StaticResource btnStyle1}" Foreground="White" FontSize="20" HorizontalAlignment="Left" VerticalAlignment="Top" Width="30" Height="30" >
  78. <Button.Content>
  79. <Image Source="Button_Icons/xn.png" />
  80. </Button.Content>
  81. </Button>
  82. <Button Style="{StaticResource btnStyle2}" Foreground="White" FontSize="20" HorizontalAlignment="Left" VerticalAlignment="Top" Width="30" Height="30" >
  83. <Button.Content>
  84. <Image Source="Button_Icons/mxn.png" />
  85. </Button.Content>
  86. </Button>
  87. <Button Style="{StaticResource btnStyle3}" Foreground="White" FontSize="20" HorizontalAlignment="Left" VerticalAlignment="Top" Width="30" Height="30" >
  88. <Button.Content>
  89. <Image Source="Button_Icons/min.png" />
  90. </Button.Content>
  91. </Button>
  92.  
  93. </StackPanel>
  94. </Grid>
  95. </Window>

效果如下:

Style的另一个非常常用的场景是:视图切换。参考DebugLZQ前面总结DataTemplate的博文中的一个例子:6.DataTemplate的一个非常常见的应用

请关注后续整理~

Wish it helps~

WPF整理-Style的更多相关文章

  1. WPF整理-使用用户选择主题的颜色和字体

    “Sometimes it's useful to use one of the selected colors or fonts the user has chosen in theWindows ...

  2. WPF整理-自定义一个扩展标记(custom markup extension)

    "Markup extensions are used to extend the capabilities of XAML, by providing declarativeoperati ...

  3. WPF整理-XAML访问静态属性

    "XAML provides an easy way to set values of properties—type converters and the extended propert ...

  4. WPF整理-XAML构建后台类对象

    1.XAML 接触WPF的第一眼就是XAML---XAML是用来描绘界面的.其实不然! "Actually, XAML has nothing to do with UI. It's mer ...

  5. WPF 之 style文件的引用

    总结一下WPF中Style样式的引用方法. 一.内联样式: 直接设置控件的Height.Width.Foreground.HorizontalAlignment.VerticalAlignment等属 ...

  6. WPF的Style的TargetType不同写法的异同

    原文:WPF的Style的TargetType不同写法的异同 <Style TargetType="TextBlock"> <Setter Property=&q ...

  7. WPF中Style文件的引用——使用xaml代码或者C#代码动态加载

    原文:WPF中Style文件的引用--使用xaml代码或者C#代码动态加载 WPF中控件拥有很多依赖属性(Dependency Property),我们可以通过编写自定义Style文件来控制控件的外观 ...

  8. WPF 中style文件的引用

    原文:WPF 中style文件的引用 总结一下WPF中Style样式的引用方法: 一,内联样式: 直接设置控件的Height.Width.Foreground.HorizontalAlignment. ...

  9. WPF整理-使用逻辑资源

    "Traditional application resources consist of binary chunks of data, typically representing thi ...

随机推荐

  1. C#获取屏幕鼠标所指点的颜色

    有时候要获取屏幕某一点的坐标颜色值,可以如下实现: 在VS2012中创建一个C#的Windows窗口应用程序,然后在Form上添加PictureBox和Button两个控件,并加入以下代码. //需要 ...

  2. python 新手遇到的问题

    作为新手,我把之前遇到的问题贴出来 错误提示1: TypeError: unbound method a() must be called with A instance as first argum ...

  3. 5 Convert Sorted List to Binary Search Tree_Leetcode

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  4. Python-面向对象

    面向过程变成:函数式变成,C程序等 面向对象编程:C++,Java,Python等   类和对象: 类:是对事物的抽象,比如人类.球类 对象:是类的一个实例,比如足球.篮球   实例说明: 球类可以对 ...

  5. Web.xml配置参数详解

    1 定义头和根元素 部署描述符文件就像所有XML文件一样,必须以一个XML头开始.这个头声明可以使用的XML版本并给出文件的字符编码.DOCYTPE声明必须立即出现在此头之后.这个声明告诉服务器适用的 ...

  6. python遍历一个网段的ip地址

    def ip2num(ip):#ip to int num lp = [int(x) for x in ip.split('.')] return lp[0] << 24 | lp[1] ...

  7. 调用WCF不需要添加服务引用,使用一个WCFHelper类就可以

    效果图: 调用过程: string WCFURL = "http://localhost:100/Service1.svc"; UserRequest user = new Use ...

  8. LINUX 根目录说明

    linux目录:/bin      bin是Binary的缩写.这个目录存放着最经常使用的命令./boot 这里存放的是启动Linux时使用的一些核心文件,包括一些连接文件以及镜像文件./data / ...

  9. 转:WaitForSingleObject()函数、WaitForMultipleObject()函数

    http://blog.csdn.net/xiaobai1593/article/details/6672193 在多线程下面,有时候我们会希望等待某一线程完成了再继续做其他事情,要实现这个目的,可以 ...

  10. mybatis自增长插入id

    第一种: <insert id="insertUser" parameterClass="ibatis.User"> <selectKey r ...