DynamicResource帮助定义一个程序中的通用资源
2008-09-28 11:07
253 查看
通常情况下,我们需要在我们的应用程序界面中使用相同风格的界面,比如风格一样的Button,ListView等等,我们不可能在每个Window或者Page中去定义这些风格的资源,这时,我们可以通过DynamicResource解决问题。 下面我们开始一步一步定义DynamicResource及使用。 1. 新建一个skins.xaml文件,在这个文件中定义我们的公共资源。<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="MyTextBoxStyle" TargetType="{x:Type TextBox}">
<Setter Property="FontFamily" Value="Arial"></Setter>
<Setter Property="FontSize" Value="23"></Setter>
<Setter Property="Foreground" Value="Blue"></Setter>
</Style>
<Style x:Key="MyTextBlockStyle" TargetType="{x:Type TextBlock}">
<Setter Property="FontFamily" Value="Arial"></Setter>
<Setter Property="FontSize" Value="15"></Setter>
<Setter Property="Background" Value="LightBlue"></Setter>
<Setter Property="Foreground" Value="Red"></Setter>
</Style>
<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
<Setter Property="FontFamily" Value="Arial"></Setter>
<Setter Property="FontSize" Value="15"></Setter>
<Setter Property="Background" Value="LightBlue"></Setter>
<Setter Property="Foreground" Value="Green"></Setter>
</Style>
</ResourceDictionary>
这里一共定义了三个公共资源,“MyTextBoxStyle”用于TextBox控件,“MyTextBlockStyle”用于TextBlock控件,“MyButtonStyle”用于Button控件。我们设置TextBox字体颜色为Blue,字体为Arial,大小为23;TextBlock背景为LightBlue,字体颜色为Red;Button背景为LightBlue,字体颜色为Green。
然后,在我们需要用到这些资源的地方,比如一个Window中,我们在MainWindow.xaml中:<Window x:Class="DynamicResource.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="200" Width="300" Background="LightBlue">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="skins.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*"/>
<ColumnDefinition Width="5*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="1" Grid.Column="0" Style="{DynamicResource MyTextBlockStyle}">测试用的TextBlock</TextBlock>
<TextBox Grid.Row="1" Grid.Column="1" Style="{DynamicResource MyTextBoxStyle}" HorizontalAlignment="Left" Width="100"></TextBox>
<Button Grid.Row="2" Grid.Column="1" Style="{DynamicResource MyButtonStyle}" Width="55" Height="30" Content="确定"></Button>
</Grid>
</Window>
其中<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="skins.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
为引入动态资源。
<TextBlock Grid.Row="1" Grid.Column="0" Style="{DynamicResource MyTextBlockStyle}">测试用的TextBlock</TextBlock>
<TextBox Grid.Row="1" Grid.Column="1" Style="{DynamicResource MyTextBoxStyle}" HorizontalAlignment="Left" Width="100"></TextBox>
<Button Grid.Row="2" Grid.Column="1" Style="{DynamicResource MyButtonStyle}" Width="55" Height="30" Content="确定"></Button>
为相应控件使用相应的DynamicResource。
我们来看看实际效果
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="MyTextBoxStyle" TargetType="{x:Type TextBox}">
<Setter Property="FontFamily" Value="Arial"></Setter>
<Setter Property="FontSize" Value="23"></Setter>
<Setter Property="Foreground" Value="Blue"></Setter>
</Style>
<Style x:Key="MyTextBlockStyle" TargetType="{x:Type TextBlock}">
<Setter Property="FontFamily" Value="Arial"></Setter>
<Setter Property="FontSize" Value="15"></Setter>
<Setter Property="Background" Value="LightBlue"></Setter>
<Setter Property="Foreground" Value="Red"></Setter>
</Style>
<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
<Setter Property="FontFamily" Value="Arial"></Setter>
<Setter Property="FontSize" Value="15"></Setter>
<Setter Property="Background" Value="LightBlue"></Setter>
<Setter Property="Foreground" Value="Green"></Setter>
</Style>
</ResourceDictionary>
这里一共定义了三个公共资源,“MyTextBoxStyle”用于TextBox控件,“MyTextBlockStyle”用于TextBlock控件,“MyButtonStyle”用于Button控件。我们设置TextBox字体颜色为Blue,字体为Arial,大小为23;TextBlock背景为LightBlue,字体颜色为Red;Button背景为LightBlue,字体颜色为Green。
然后,在我们需要用到这些资源的地方,比如一个Window中,我们在MainWindow.xaml中:<Window x:Class="DynamicResource.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="200" Width="300" Background="LightBlue">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="skins.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*"/>
<ColumnDefinition Width="5*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="1" Grid.Column="0" Style="{DynamicResource MyTextBlockStyle}">测试用的TextBlock</TextBlock>
<TextBox Grid.Row="1" Grid.Column="1" Style="{DynamicResource MyTextBoxStyle}" HorizontalAlignment="Left" Width="100"></TextBox>
<Button Grid.Row="2" Grid.Column="1" Style="{DynamicResource MyButtonStyle}" Width="55" Height="30" Content="确定"></Button>
</Grid>
</Window>
其中<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="skins.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
为引入动态资源。
<TextBlock Grid.Row="1" Grid.Column="0" Style="{DynamicResource MyTextBlockStyle}">测试用的TextBlock</TextBlock>
<TextBox Grid.Row="1" Grid.Column="1" Style="{DynamicResource MyTextBoxStyle}" HorizontalAlignment="Left" Width="100"></TextBox>
<Button Grid.Row="2" Grid.Column="1" Style="{DynamicResource MyButtonStyle}" Width="55" Height="30" Content="确定"></Button>
为相应控件使用相应的DynamicResource。
我们来看看实际效果
相关文章推荐
- 怎样把一个程序中的对话框资源和对话框类移植到另一个程序中
- 采用C++的ACE库实现的一个通用的udp通信服务器程序
- asp vbscript常用函数语语法备查,一个帮助文档,对写asp程序很有帮助的
- 一个可以通用的防注入脚本程序
- 一个操作数据库通用帮助类
- 发布一个通用面板程序(htc)
- 最近写的一个小程序【U影网】--完美融合BT与Emule资源搜索站
- Sprite Kit教程:制作一个通用程序 2
- Sprite Kit教程:制作一个通用程序 2
- 请问如何将一个DLL作为资源加入一个程序中?
- Linux平台下用curses库写的一个通用的图形化菜单生成程序
- 第十三周【任务三】【任务3】写一个程序,定义抽象基类Shape,由它派生出3个派生类,Circle(圆形)、Rectangle(矩形)、Triangle(三角形)。
- 《第十三周任务三》写一个程序,定义抽象基类Shape,由它派生出3个派生类,Circle(圆形)、Rectangle(矩形)、Triangle(三角形)。用如下的mian()函数,求出定义的几个几何体
- c#串口开发源代码——这是我09年一个写的一个非常简陋的通用的串口调试程序
- 采用C++的ACE库实现的一个通用的C/S架构通信程序(最终版)
- 采用C++的ACE库实现的一个通用的C/S架构通信程序
- 【郝斌数据结构自学笔记】49-52_递归的定义和不同函数之间相互调程序举例_一个函数自己调自己程序举例
- 网址重写 由一个任意或是自己定义的地址转到指定的处理程序 4000 上来
- Delphi 中将一些 Dll等生成资源文件打包成一个独立的EXE程序方法步骤
- 一个比较通用的makefile,写一些小程序基本够用了