繁体   English   中英

在XAML中设置全局样式

[英]Setting global style in XAML

有什么方法可以在xaml中的资源字典中设置全局样式,而不必在受影响的元素中指定任何特殊设置?

例如,假设我有resdictionary.xaml文件,

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

 </ResourceDictionary>

然后我有正常的window.xaml文件:

<Window x:Class="App.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
    Title="MainWindow" Height="500" Width="800" MinWidth="500" MinHeight="350">
<Window.Resources>
    <ResourceDictionary Source="resdictionary.xaml" />
</Window.Resources>   

 <Button Content="Button 1"/>
 <Button Content="Button 2"/>
 <Button Content="Button 3"/>
 </Window>

我希望所有按钮都具有黑白背景渐变色和Calibri字体。 有没有一种方法可以在resdictionary.xaml中指定它而不必更改window.xaml文件?

您可以创建一个styles.xaml页面并在其中添加所有样式

<Style x:Key="SubmitButtonStyle" TargetType="Button">
    <Setter Property="Height">
        <Setter.Value>28px</Setter.Value>
    </Setter>
    <Setter Property="Width">
        <Setter.Value>90px</Setter.Value>
    </Setter>
    <Setter Property="BorderThickness">
        <Setter.Value>1</Setter.Value>
    </Setter>
    <Setter Property="BorderBrush">
        <Setter.Value>#000000</Setter.Value>
    </Setter>
    <Setter Property="Background">
        <Setter.Value>#0073c6</Setter.Value>
    </Setter>
    <Setter Property="FontFamily" >
        <Setter.Value>Segoe UI</Setter.Value>
    </Setter>
    <Setter Property="FontSize">
        <Setter.Value>12px</Setter.Value>
    </Setter>
</Style>

您可以使用以下代码在Windows中引用styles.xaml。

    <UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary  Source="../Resources/Styles.xaml"></ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

您可以按照以下方式在页面中使用样式

<Button Style="{StaticResource TelerikRadSubmitButtonStyle}"/>

希望这可以帮助。

您可以按照Dream Coder显示的方式进行操作。 但是那样,您必须为每个按钮设置style 如果未将style设置为按钮,则该button将使用默认的Windows样式。 为此,您必须从style删除x:Key

<Style TargetType="{x:Type Button}">
    <Setter Property="Background">
        <Setter.Value>
             <LinearGradientBrush EndPoint="0.5,1"
                     StartPoint="0.5,0">
                <GradientStop Color="#000000" />
                <GradientStop Color="#FFFFFF"
                      Offset="1" />
             </LinearGradientBrush>
        </Setter.Value>
    </Setter>
    <Setter Property="FontFamily" 
            Value="Calibri" />
</Style>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM