【问题标题】:How to connect Themes/Generic.xaml to window1.xaml?如何将 Themes/Generic.xaml 连接到 window1.xaml?
【发布时间】:2009-02-10 14:22:31
【问题描述】:

我在 Themes/Generic.xaml 中有以下按钮样式,我希望它适用于我的 WPF 应用程序中的所有按钮。

例如,我如何将它连接到我的 window1.xaml?

<Style TargetType="{x:Type Button}">
  <Setter Property="SnapsToDevicePixels" Value="true"/>
  <Setter Property="OverridesDefaultStyle" Value="true"/>
  <Setter Property="MinHeight" Value="23"/>
  <Setter Property="MinWidth" Value="75"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type Button}">
        <Border 
          x:Name="Border"  
          CornerRadius="2" 
          BorderThickness="1"
          Background="#C0C0C0"
          BorderBrush="#404040">
          <ContentPresenter 
            Margin="2"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            RecognizesAccessKey="True"/>
        </Border>
        <ControlTemplate.Triggers>
          <Trigger Property="IsKeyboardFocused" Value="true">
            <Setter TargetName="Border" Property="BorderBrush" Value="#202020" />
          </Trigger>
          <Trigger Property="IsDefaulted" Value="true">
            <Setter TargetName="Border" Property="BorderBrush" Value="#202020" />
          </Trigger>
          <Trigger Property="IsMouseOver" Value="true">
            <Setter TargetName="Border" Property="Background" Value="#808080" />
          </Trigger>
          <Trigger Property="IsPressed" Value="true">
            <Setter TargetName="Border" Property="Background" Value="#E0E0E0" />
            <Setter TargetName="Border" Property="BorderBrush" Value="#606060" />
          </Trigger>
          <Trigger Property="IsEnabled" Value="false">
            <Setter TargetName="Border" Property="Background" Value="#EEEEEE" />
            <Setter TargetName="Border" Property="BorderBrush" Value="#AAAAAA" />
            <Setter Property="Foreground" Value="#888888"/>
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

【问题讨论】:

  • 我知道这是一个老问题...但我建议如果您只想更改默认按钮样式...您将上述 xaml 移动到您的 App.xaml 文件中。 Generic.xaml 具有作为 WPF 主题样式的后备机制的特定目的。请参阅下面的链接(按歌曲)指向另一个 StackOverflow 帖子,其中包含有关此主题的更多信息。
  • 见鬼,Muad'Dib 的回答在下面......通过在......而不是 Generic.xaml 中合并 DefaultStyles.xaml,隐含地暗示了我的意思。 :-)

标签: wpf styles


【解决方案1】:

在您的 Widow1.xaml(或您的 App.xaml,更改为)中......

<Window1.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="DefaultStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window1.Resources>

【讨论】:

    猜你喜欢
    • 2022-01-11
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多