WPF开发样式文件的四种引用方式

545
补充展位 Pages_Weblog_Get#0
文章摘要
此内容由人工摘要内容,并由AI根据文章内容进行润色
暂无内容

WPF样式文件的四种引用方式

XAML是专门用于WPF技术中的UI设计语言,全称Extensible Application Markup Language (可扩展应用程序标记语言)。

以下是WPF中四种引用方式

一、外联引用

在App.xaml文件中的 Applictaion.Resources 节点下添加 ResourceDictionary节点:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/应用名称;component/Theme/Style.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

二、内联引用

<Button Height="30" Width="60" Background="Green" Foreground="White"></Button>

三、 嵌入式引用

在页面或者窗体的资源节点下面(Window.Resources或者Page.Resources)添加Style样式

<Window.Resources>
    <Style x:Key="myBtnStyle" TargetType="{x:Type Button}">
        <Setter Property="Height" Value="72" />
        <Setter Property="Width" Value="150" />
        <Setter Property="Foreground" Value="Red" />
        <Setter Property="Background" Value="Black" />
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="VerticalAlignment" Value="Top" />
    </Style>
</Window.Resources>

四、代码方式动态加载

Button test_btn =new  Button();
test_btn.SetValue(Button.StyleProperty, Application.Current.Resources["样式资源"]);
补充展位
Pages_Weblog_Get#d8cd4966-2210-4109-8ca4-99ff935b3908
补充展位 Pages_Weblog_Get#1
补充展位 Pages_Weblog_Get#2
专题推荐
暂无内容
补充展位 Pages_Weblog_Get#3