您的位置:首页 > 其它

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。
我们来看看实际效果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐