DynamicResource帮助定义一个程序中的通用资源

4 篇文章 0 订阅
    通常情况下,我们需要在我们的应用程序界面中使用相同风格的界面,比如风格一样的Button,ListView等等,我们不可能在每个Window或者Page中去定义这些风格的资源,这时,我们可以通过DynamicResource解决问题。
    下面我们开始一步一步定义DynamicResource及使用。
    1. 新建一个skins.xaml文件,在这个文件中定义我们的公共资源。
  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  3.     <Style x:Key="MyTextBoxStyle" TargetType="{x:Type TextBox}">
  4.         <Setter Property="FontFamily" Value="Arial"></Setter>
  5.         <Setter Property="FontSize" Value="23"></Setter>
  6.         <Setter Property="Foreground" Value="Blue"></Setter>
  7.     </Style>
  8.     <Style x:Key="MyTextBlockStyle" TargetType="{x:Type TextBlock}">
  9.         <Setter Property="FontFamily" Value="Arial"></Setter>
  10.         <Setter Property="FontSize" Value="15"></Setter>
  11.         <Setter Property="Background" Value="LightBlue"></Setter>
  12.         <Setter Property="Foreground" Value="Red"></Setter>
  13.     </Style>
  14.     <Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
  15.         <Setter Property="FontFamily" Value="Arial"></Setter>
  16.         <Setter Property="FontSize" Value="15"></Setter>
  17.         <Setter Property="Background" Value="LightBlue"></Setter>
  18.         <Setter Property="Foreground" Value="Green"></Setter>
  19.     </Style>
  20. </ResourceDictionary>
    这里一共定义了三个公共资源,“MyTextBoxStyle”用于TextBox控件,“MyTextBlockStyle”用于TextBlock控件,“MyButtonStyle”用于Button控件。我们设置TextBox字体颜色为Blue,字体为Arial,大小为23;TextBlock背景为LightBlue,字体颜色为Red;Button背景为LightBlue,字体颜色为Green。
然后,在我们需要用到这些资源的地方,比如一个Window中,我们在MainWindow.xaml中:
  1. <Window x:Class="DynamicResource.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="200" Width="300" Background="LightBlue">
  5.     <Window.Resources>
  6.         <ResourceDictionary>
  7.             <ResourceDictionary.MergedDictionaries>
  8.                 <ResourceDictionary Source="skins.xaml"></ResourceDictionary>
  9.             </ResourceDictionary.MergedDictionaries>
  10.         </ResourceDictionary>
  11.     </Window.Resources>
  12.     <Grid>
  13.         <Grid.RowDefinitions>
  14.             <RowDefinition Height="30"/>
  15.             <RowDefinition Height="30"/>
  16.             <RowDefinition Height="*"/>
  17.         </Grid.RowDefinitions>
  18.         <Grid.ColumnDefinitions>
  19.             <ColumnDefinition Width="5*"/>
  20.             <ColumnDefinition Width="5*"/>
  21.         </Grid.ColumnDefinitions>
  22.         <TextBlock Grid.Row="1" Grid.Column="0" Style="{DynamicResource MyTextBlockStyle}">测试用的TextBlock</TextBlock>
  23.         <TextBox Grid.Row="1" Grid.Column="1" Style="{DynamicResource MyTextBoxStyle}" HorizontalAlignment="Left" Width="100"></TextBox>
  24.         <Button Grid.Row="2" Grid.Column="1" Style="{DynamicResource MyButtonStyle}" Width="55" Height="30" Content="确定"></Button>
  25.     </Grid>
  26. </Window>
其中
  1. <Window.Resources>
  2.         <ResourceDictionary>
  3.             <ResourceDictionary.MergedDictionaries>
  4.                 <ResourceDictionary Source="skins.xaml"></ResourceDictionary>
  5.             </ResourceDictionary.MergedDictionaries>
  6.         </ResourceDictionary>
  7. </Window.Resources>
为引入动态资源。

  1. <TextBlock Grid.Row="1" Grid.Column="0" Style="{DynamicResource MyTextBlockStyle}">测试用的TextBlock</TextBlock>
  2.         <TextBox Grid.Row="1" Grid.Column="1" Style="{DynamicResource MyTextBoxStyle}" HorizontalAlignment="Left" Width="100"></TextBox>
  3.         <Button Grid.Row="2" Grid.Column="1" Style="{DynamicResource MyButtonStyle}" Width="55" Height="30" Content="确定"></Button>
为相应控件使用相应的DynamicResource。

我们来看看实际效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值