原文:WPF 中style文件的引用

总结一下WPF中Style样式的引用方法:

一,内联样式:

直接设置控件的Height、Width、Foreground、HorizontalAlignment、VerticalAlignment等属性。以设置一个Botton控件的样式为例,如:

复制代码

<Grid x:Name="ContentPanel" >

<Button Content="Button" Name="btnDemo"

Height="72"

Width="150"

Foreground="White"

Background="Blue"

HorizontalAlignment="Left"

VerticalAlignment="Top"

Margin="170,132,0,0"

Grid.Row="1" />

</Grid>

这种方式比较简单,但是代码不能复用。

二,嵌入样式:

在页面<Window.Resources>节点下添加样式,然后在需要的控件上设置Style属性。还是以上面那个Botton控件为例。

1,在页面<Window.Resources>节点下添加一个Key值叫“myBtnStyle”的样式

复制代码

<Window.Resources>

<Style x:Key="myBtnStyle" TargetType="{x:Type Button}">

<Setter Property="Height" Value="72" />

<Setter Property="Width" Value="150" />

<Setter Property="Foreground" Value="Red" />

<Setter Property="Background" Value="Black" />

<Setter Property="HorizontalAlignment" Value="Left" />

<Setter Property="VerticalAlignment" Value="Top" />

</Style>

</Window.Resources>

2, 设置Botton控件的Style属性为"{StaticResource BtnStyle}"

<Grid x:Name="ContentPanel" >

<Button Content="Button" Name="btnDemo"

Style="{StaticResource BtnStyle}"/>

</Grid>

解释一下,TargetType="{x:Type Button}"指定了该样式适用于Botton类型的控件,Key="myBtnStyle"如果不设置该值,则该样式将适用于所有的Botton控件,而设置了其值为“myBtnStyle”,则只用于设置了
Style="{StaticResource
myBtnStyle}"的Botton控件。这就好比CSS中的元素选择器和类选择器。

这种方式可以使得单个页面上的控件能够复用一个样式,比第一种方式面向对象了一步。

三,外联样式:

1,新建一个.xaml资源文件,如/Theme/Style.xaml

2, 在Style.xaml文件里编写样式代码

Style.xaml:

<ResourceDictionary

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:System="clr-namespace:System;assembly=mscorlib">

<Style x:Key="myBtnStyle" TargetType="Button">

<Setter Property="Height" Value="72" />

<Setter Property="Width" Value="150" />

<Setter Property="Foreground" Value="White" />

<Setter Property="Background" Value="Blue" />

<Setter Property="HorizontalAlignment" Value="Left" />

<Setter Property="VerticalAlignment" Value="Top" />

</Style>

</ResourceDictionary>

3,在App.xaml文件的<Application.Resources>

或者普通页面的<Window.Resources>

或者用户控件的 <UserControl.Resources> 节点下

添加相应的ResourceDictionary,配置引用Style.xaml:

app.xaml:

<Application.Resources>

<ResourceDictionary>

<ResourceDictionary.MergedDictionaries>

<ResourceDictionary Source="/应用名称;component/Theme/Style.xaml"/>

<!--<ResourceDictionary Source="Resources/BtnStyle2.xaml"/>

</ResourceDictionary.MergedDictionaries>

</ResourceDictionary>

</Application.Resources>

或者MainWindow.xaml:

<Window.Resources>

<ResourceDictionary>

<ResourceDictionary.MergedDictionaries>

<ResourceDictionary Source="Theme/BtnStyle.xaml"/>

</ResourceDictionary.MergedDictionaries>

</ResourceDictionary>

</Window.Resources>

<ResourceDictionary.MergedDictionaries>节点下可以添加多个资源文件

这种方式相比前面两种使得样式和结构又更进一步分离了。

在App.xaml引用,是全局的,可以使得一个样式可以在整个应用程序中能够复用。在普通页面中引用只能在当前页面上得到复用。

4, 设置Botton控件的Style属性为"{StaticResource myBtnStyle}" 和上面的一样。

四,用C#代码动态加载资源文件并设置样式

1,新建资源文件:同上面的1,2两步。

2,在后台编写代码

ResourceDictionary resourceDictionary =newResourceDictionary();

Application.LoadComponent(resourceDictionary, new Uri("/PhoneApp1;component/Resources/BtnStyle.xaml", UriKind.Relative));

Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);

//以上几行代码表示将我们自定义的样式加载到应用程序的资源字典中。

this.btnDemo.SetValue(Button.StyleProperty, Application.Current.Resources["BtnStyle"]);

各位大虾见笑了,呵呵!!!

WPF 中style文件的引用的更多相关文章

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

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

  2. WPF 之 style文件的引用

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

  3. WPF中Style文件引用另一个Style文件中的样式

    第1种方法: 直接在当前Style文件(*.xaml)文件中使用: <ResourceDictionary.MergedDictionaries>来进行合并 <!-- 关键是注意so ...

  4. WPF中使用文件浏览对话框的几种方式

    原文:WPF中使用文件浏览对话框的几种方式 WPF本身并没有为我们提供文件浏览的控件, 也不能直接使用Forms中的控件,而文件浏览对话框又是我们最常用的控件之一. 下面是我实现的方式 方式1: 使用 ...

  5. 在WPF中使用文件夹选择对话框

    开发中有时会想实现"选择某个文件夹"的效果: 在WPF中,使用Microsoft.Win32.OpenFileDialog只能选择文件,FolderBrowserDialog只能用 ...

  6. WPF中TextBox文件拖放问题

    在WPF中,当我们尝试向TextBox中拖放文件,从而获取其路径时,往往无法成功(拖放文字可以成功).造成这种原因关键是WPF的TextBox对拖放事件处理机制的不同,具体可参考这篇文章Textbox ...

  7. 解决WPF中TextBox文件拖放问题

    在WPF中,当我们尝试向TextBox中拖放文件,从而获取其路径时,往往无法成功(拖放文字可以成功).造成这种原因关键是WPF的TextBox对拖放事件处理机制的不同,具体可参考这篇文章Textbox ...

  8. WPF中选择文件和选择文件夹的方法

    最近从winform转WPF,遇到了各种各样的问题.然而网上的关于WPF的资料少之又少,甚至连基本的文件选择操作,百度搜索的首页都没有一个比较好的方法.所以,踩了几个坑之后,我把我得到的方法分享给大家 ...

  9. VS2012添加对DirectX SDK中需要文件的引用

    error LNK2019: 无法解析的外部符号 _DirectDrawCreateEx@16,该符号在函数 "int __cdecl DD_Init(int,int,int)" ...

随机推荐

  1. JNI:no implementation found in native...

    一  javah引发的问题 BUG:D/dalvikvm( 1704): Trying to load lib /data/data/com.ulang/lib/libulangaudio.so 0x ...

  2. [React] Understand React.Children Utilities

    The data contained in this.props.children is not always what you might expect. React provides React. ...

  3. .Net Core Socket 压力测试

    原文:.Net Core Socket 压力测试 .Net Core Socket 压力测试 想起之前同事说go lang写的push service单机可以到达80万连接,于是就想测试下.Net C ...

  4. java完美equals方法代码段

    public boolean equals(Object otherObject) { if(this == otherObject) { // 检測this与otherObject是否引用同一个对象 ...

  5. BZOJ 2096 Pilots - 单调队列STL(deque)

    传送门 分析: 单调队列:维护两个递增.递减的队列,每次都加入新元素并更新,如果最大值(递减队首)-最小值(递增队首) > k,那么将最左段更新为前面两者中较前的那一个,并弹掉.用deque可以 ...

  6. TensorFlow 下 mnist 数据集的操作及可视化

    from tensorflow.examples.tutorials.mnist import input_data 首先需要连网下载数据集: mnsit = input_data.read_data ...

  7. BZOJ 2818 Gcd 线性欧拉筛(Eratosthenes银幕)

    标题效果:定整N(N <= 1e7),乞讨1<=x,y<=N和Gcd(x,y)素数的数(x,y)有多少.. 思考:推,. 建立gcd(x,y) = p,然后,x / p与y / p互 ...

  8. Parallel.For

    Parallel.For 你可能忽视的一个非常实用的重载方法    说起Parallel.For大家都不会陌生,很简单,不就是一个提供并行功能的for循环吗? 或许大家平时使用到的差不多就是其中最简单 ...

  9. 给博客签上CC协议

    大家都知道开源软件.通过开放源代码的方式,允许用户学习.修改.增进提高这些软件质量.软件界的开源协议很多,比如常见的 Apache,BSD,GPL 等等.这是一种充分利用网络的便利性,鼓励分享和创新的 ...

  10. Msg DisPatch

    一天写了个Carlife 协议数据分流器 #include <stdio.h> #include <string.h> typedef unsigned char uint8_ ...