【问题标题】:How to make a Style generic so I can use it in my entire application?如何使样式通用,以便我可以在整个应用程序中使用它?
【发布时间】:2015-09-11 12:43:10
【问题描述】:

我在这里搜索了 SO,找不到明确的答案来解释如何设置“样式资源”。在我的情况下,我的对话框控制几个按钮和列表,以及我想为其设置通用主题/样式的其他各种控件。类似于在 HTML 中使用 CSS 文件的方式。

为了在这个例子中简单起见,我想在我的所有按钮上全面使用一种样式。但是,我不希望在我的 UI 布局的 xaml 中包含所有这些样式资源。我想将样式移动到只包含样式的通用 xaml 资源文件中,这样我也可以轻松地将它们引用到整个工具中的其他 wpf 对话框中。

如何设置它以使用包含工具中各种控件样式的通用资源文件?然后能够在我的 xaml UI 中引用和使用该样式。 p>

谢谢

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="200" Width="200">

    <Window.Resources>
        <Style TargetType="Button" x:Key="CoolButton" >
            <Setter Property="Margin" Value="1,2,1,2"/>
            <Setter Property="Background" Value="LightBlue"/>
            <Setter Property="HorizontalAlignment" Value="Left"/>
            <Setter Property="FontSize" Value="12" />
            <Setter Property="FontFamily" Value="Calibri" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border Background="{TemplateBinding Background}"
                    BorderBrush="Lavender" BorderThickness="5" CornerRadius="6,0,6,0" x:Name="bd">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                    Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True" />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="bd" Property="Background" Value="LightGray"/>
                                <Setter Property="Foreground" Value="White" />
                                <Setter Property="Cursor" Value="Hand" />
                            </Trigger>

                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <StackPanel>
        <Button Style="{StaticResource CoolButton}" Content="Button" Margin="2" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75"/>
        <Button Style="{StaticResource CoolButton}" Content="Button" Margin="2" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75"/>
        <CheckBox Margin="2" Content="Great"></CheckBox>

    </StackPanel>
</Window>

附带说明为什么在资源样式 xaml 中使用颜色变量不起作用?

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

        <!--COLORS-->
        <Color x:Key="AccentColor">#CC4021</Color>


        <Style TargetType="Button">
            <Setter Property="Foreground" Value="{StaticResource AccentColor}"/>
        </Style>

</ResourceDictionary>

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    将特定于实例的样式变为通用样式需要遵循几个步骤。

    1. 删除Key。这将使样式用于每个按钮:

      <Style TargetType="Button">
      
    2. 将其移至资源文件,例如Default.xaml

      <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      >
          <Style TargetType="Button">
              ...
          </Style>
      </ResourceDictionary>
      
    3. 包括从中心点对资源的引用,例如 App.xaml,它将加载资源。 App.xaml 将导致样式一次性在应用程序范围内使用:

      <Application x:Class="..."
                   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
          <Application.Resources>
              <ResourceDictionary>
                  <ResourceDictionary.MergedDictionaries>
                      <ResourceDictionary Source="Default.xaml" />                 
                  </ResourceDictionary.MergedDictionaries>
              </ResourceDictionary>
          </Application.Resources>
      </Application>
      

    【讨论】:

    • 这似乎不会影响任何子窗口,只会影响我的主应用程序窗口。我怎样才能让我的 xaml 也影响这些?
    • 我应该这么想。我明天需要一些时间来测试。
    • 上面已编辑问题,添加了资源文件不允许颜色变量的问题。
    • @JokerMartini 在这里工作正常。如果我从第一个窗口打开第二个窗口,它只会加载。颜色也有效。刚刚对我的代码进行了一些更改,因为我做了错误的复制/粘贴。
    • 好的,今晚我再去看看。您是否尝试了颜色变量,它对我来说是错误的。
    猜你喜欢
    • 2011-12-28
    • 2011-12-15
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    • 2017-10-15
    相关资源
    最近更新 更多