WPF 样式和资源 给某个元素设置Style样式

来源:互联网 发布:安卓改iphone6在线软件 编辑:程序博客网 时间:2024/04/30 09:35

一、简单实例。文件分布。


二、Style文件类型是这样的



三、WPF View页面(MainWindow.xaml)

需要写这代码;

<Window x:Class="WPFiveStar.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Style/Style.xaml"/>                      //红色字体是实现重点
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

    
    <Grid>
        <Button Content="Click me" x:Name="Button_key" Width="150" Height="50" Style="{StaticResource Button_key}"/>
    </Grid>
</Window>

四、Style页面

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">


    <Style x:Key="Button_key" TargetType="Button">
        <Setter Property="Background" Value="Red"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="FontSize" Value="14"/>
    </Style>


</ResourceDictionary>



0 0
原创粉丝点击