WPF 使用ComponentResourceKey,隐藏代码,generic创建项目共享Resource
2012-08-31 15:31
519 查看
Generic 代码 :
Styles1.xaml代码:
Styles1.cs代码
ResourceStyle.xaml 代码:
ResourceLib.csproj 实现隐藏代码
显示效果
项目结构:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:ResourceLib"> <ResourceDictionary.MergedDictionaries > <ResourceDictionary Source="/ResourceLib;component/Styles1.xaml"></ResourceDictionary> </ResourceDictionary.MergedDictionaries> <Style TargetType="{x:Type local:CustomControl1}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:CustomControl1}"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary>
Styles1.xaml代码:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:ResourceLib"> <Style x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:Styles1},ResourceId=bigFontStyle}" > <Setter Property="Control.FontFamily" Value="Times New Roman"></Setter> <Setter Property="Control.FontSize" Value="18"></Setter> <Setter Property="Control.FontWeight" Value="Bold"></Setter> </Style> </ResourceDictionary>
Styles1.cs代码
public partial class Styles1 { public static ComponentResourceKey BigFontStyleKey { get { return new ComponentResourceKey(typeof(Styles1), "bigFontStyle"); } } }
ResourceStyle.xaml 代码:
<Window x:Class="WPFLearning.StyleTemplates.ResourceStyle" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:rl="clr-namespace:ResourceLib;assembly=ResourceLib" Title="ResourceStyle" Height="300" Width="300"> <Grid> <Button Style="{DynamicResource {x:Static rl:Styles1.BigFontStyleKey}}">Hi, i use a component sytle</Button> </Grid> </Window>
ResourceLib.csproj 实现隐藏代码
<Compile Include="Styles1.cs"> <DependentUpon>Styles1.xaml</DependentUpon> </Compile>
显示效果
项目结构:
相关文章推荐
- QT中静态库的生成与使用——创建共享库代码,附例子
- WPF中的ComponentResourceKey 标记扩展
- 第一次使用GIT,创建仓库并提交项目代码
- [原译]如何创建WPF用户控件&在WPF项目中使用
- mac上使用github命令提交代码实例(三)创建Android项目
- 第一次使用GIT,创建仓库并提交项目代码
- wpf - ComponentResourceKey and it use in Author...
- GitHub基本使用:通过eclipse创建、共享项目到GitHub
- WPF-使用代码创建Grid行与列,并将控件添加到Grid中的指定行指定列
- 只使用代码创建WPF应用程序
- WPF使用后台C#代码创建Grid
- F# 与 WPF 二 之使用纯后台代码创建WPF窗体
- Subversion创建项目及项目间共享代码
- 使用NDK创建及配置C++程序(原生纯C++项目,不包含JAVA代码)
- 使用django创建项目最终代码
- 使用Ant构建项目调试代码时无法设置断点
- CodeSmith 使用教程(15) 为Yii Framework 创建生成ActiveRecord的代码模板
- 使用springboot创建web项目(一),登录页面
- 在vue项目创建的后初始化首次使用stylus安装方法分享
- 使用网盘,创建可共享的SVN版本控制系统( 也支持Git)