2011-03-31 66 views
8

我的WPF应用程序中有列表元素上的选择样式有问题。每个列表我WPF得到深蓝色背景颜色和白色文本颜色。WPF - 将WPF应用程序中的列表更改为默认Windows 7样式的选择样式

  1. 为什么是默认的Windows 7风格的选择(例如Windows资源管理器文件选择)不会对Windows 7 WPF应用程序默认选择的风格?

  2. 如何将WPF中的选择样式更改为默认的Windows 7 Aero样式?

到目前为止,我在我的全球资源字典中有这个。但我仍然需要边界四舍五入来使它看起来相似。任何想法如何扩展这个或任何其他建议,以了解如何应用默认的Windows 7选择样式?

<LinearGradientBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" StartPoint="0,0" EndPoint="0,1"> 
<LinearGradientBrush.GradientStops> 
    <GradientStop Offset="0" Color="#FFE3F4FC"/> 
    <GradientStop Offset="0.38" Color="#FFD8EFFC"/> 
    <GradientStop Offset="0.38" Color="#FFBEE6FD"/> 
    <GradientStop Offset="1" Color="#FFA6D9F4"/> 
</LinearGradientBrush.GradientStops> </LinearGradientBrush> 

<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"></SolidColorBrush> 

回答

5

我有一篇博客文章,介绍了如何将Windows 7外观应用于ListBox here。 ListView和TreeView也包含在内,但并不完全以Windows 7为主题。

,你就需要包括相关的资源:

<!-- Hover Brushes --> 
<LinearGradientBrush x:Key="HoverBackgroundBrushKey" 
         StartPoint="0,0" 
         EndPoint="0,1"> 
    <GradientStop Color="#FCFCFC" 
        Offset="0" /> 
    <GradientStop Color="#EBF3FD" 
        Offset="1" /> 
</LinearGradientBrush> 
<SolidColorBrush x:Key="HoverOuterBorderBrushKey" 
        Color="#B8D6FB" /> 
<SolidColorBrush x:Key="HoverInnerBorderBrushKey" 
        Color="#F2F7FE" /> 

<!-- Selected (Active) Brushes --> 
<LinearGradientBrush x:Key="SelectedActiveBackgroundBrushKey" 
         StartPoint="0,0" 
         EndPoint="0,1"> 
    <GradientStop Color="#DCEBFC" 
        Offset="0" /> 
    <GradientStop Color="#C1DBFC" 
        Offset="1" /> 
</LinearGradientBrush> 
<SolidColorBrush x:Key="SelectedActiveOuterBorderBrushKey" 
        Color="#7DA2CE" /> 
<SolidColorBrush x:Key="SelectedActiveInnerBorderBrushKey" 
        Color="#EBF4FD" /> 

<!-- Selected (Inactive) Brushes --> 
<LinearGradientBrush x:Key="SelectedInactiveBackgroundBrushKey" 
         StartPoint="0,0" 
         EndPoint="0,1"> 
    <GradientStop Color="#F8F8F8" 
        Offset="0" /> 
    <GradientStop Color="#E5E5E5" 
        Offset="1" /> 
</LinearGradientBrush> 
<SolidColorBrush x:Key="SelectedInactiveOuterBorderBrushKey" 
        Color="#D9D9D9" /> 
<SolidColorBrush x:Key="SelectedInactiveInnerBorderBrushKey" 
        Color="#F0F0F0" /> 

<!-- ListBoxItem Style --> 
<Style x:Key="{x:Type ListBoxItem}" 
     TargetType="{x:Type ListBoxItem}"> 
    <Setter Property="BorderThickness" 
      Value="1" /> 
    <Setter Property="Padding" 
      Value="2,0" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
       <Grid> 
        <Border x:Name="outerBorder" 
          BorderBrush="{TemplateBinding BorderBrush}" 
          BorderThickness="{TemplateBinding BorderThickness}" 
          CornerRadius="2" 
          SnapsToDevicePixels="true"> 
         <Border x:Name="innerBorder" 
           Background="{TemplateBinding Background}" 
           BorderThickness="1" 
           CornerRadius="1" 
           Padding="{TemplateBinding Padding}" 
           SnapsToDevicePixels="true"> 
          <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
               VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
               SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> 
         </Border> 
        </Border> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsMouseOver" 
           Value="true"> 
         <Setter TargetName="outerBorder" 
           Property="BorderBrush" 
           Value="{StaticResource HoverOuterBorderBrushKey}" /> 
         <Setter TargetName="innerBorder" 
           Property="Background" 
           Value="{StaticResource HoverBackgroundBrushKey}" /> 
         <Setter TargetName="innerBorder" 
           Property="BorderBrush" 
           Value="{StaticResource HoverInnerBorderBrushKey}" /> 
        </Trigger> 
        <Trigger Property="IsSelected" 
           Value="true"> 
         <Setter TargetName="outerBorder" 
           Property="BorderBrush" 
           Value="{StaticResource SelectedActiveOuterBorderBrushKey}" /> 
         <Setter TargetName="innerBorder" 
           Property="Background" 
           Value="{StaticResource SelectedActiveBackgroundBrushKey}" /> 
         <Setter TargetName="innerBorder" 
           Property="BorderBrush" 
           Value="{StaticResource SelectedActiveInnerBorderBrushKey}" /> 
        </Trigger> 
        <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="IsSelected" 
             Value="true" /> 
          <Condition Property="Selector.IsSelectionActive" 
             Value="false" /> 
         </MultiTrigger.Conditions> 
         <Setter TargetName="outerBorder" 
           Property="BorderBrush" 
           Value="{StaticResource SelectedInactiveOuterBorderBrushKey}" /> 
         <Setter TargetName="innerBorder" 
           Property="Background" 
           Value="{StaticResource SelectedInactiveBackgroundBrushKey}" /> 
         <Setter TargetName="innerBorder" 
           Property="BorderBrush" 
           Value="{StaticResource SelectedInactiveInnerBorderBrushKey}" /> 
        </MultiTrigger> 
        <Trigger Property="IsEnabled" 
           Value="false"> 
         <Setter Property="Foreground" 
           Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" /> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<!-- ListViewItem Style --> 
<Style x:Key="{x:Type ListViewItem}" 
     TargetType="{x:Type ListViewItem}" 
     BasedOn="{StaticResource {x:Type ListBoxItem}}" /> 

<!-- Supporting TreeViewItem Resources --> 
<PathGeometry x:Key="TreeArrow"> 
    <PathGeometry.Figures> 
     <PathFigureCollection> 
      <PathFigure IsFilled="True" 
         StartPoint="0 0" 
         IsClosed="True"> 
       <PathFigure.Segments> 
        <PathSegmentCollection> 
         <LineSegment Point="0 6" /> 
         <LineSegment Point="6 0" /> 
        </PathSegmentCollection> 
       </PathFigure.Segments> 
      </PathFigure> 
     </PathFigureCollection> 
    </PathGeometry.Figures> 
</PathGeometry> 
<Style x:Key="ExpandCollapseToggleStyle" 
     TargetType="{x:Type ToggleButton}"> 
    <Setter Property="Focusable" 
      Value="False" /> 
    <Setter Property="Width" 
      Value="16" /> 
    <Setter Property="Height" 
      Value="16" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ToggleButton}"> 
       <Border Width="16" 
         Height="16" 
         Background="Transparent" 
         Padding="5,5,5,5"> 
        <Path x:Name="ExpandPath" 
          Fill="Transparent" 
          Stroke="#FF989898" 
          Data="{StaticResource TreeArrow}"> 
         <Path.RenderTransform> 
          <RotateTransform Angle="135" 
               CenterX="3" 
               CenterY="3" /> 
         </Path.RenderTransform> 
        </Path> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsMouseOver" 
           Value="True"> 
         <Setter TargetName="ExpandPath" 
           Property="Stroke" 
           Value="#FF1BBBFA" /> 
         <Setter TargetName="ExpandPath" 
           Property="Fill" 
           Value="Transparent" /> 
        </Trigger> 

        <Trigger Property="IsChecked" 
           Value="True"> 
         <Setter TargetName="ExpandPath" 
           Property="RenderTransform"> 
          <Setter.Value> 
           <RotateTransform Angle="180" 
                CenterX="3" 
                CenterY="3" /> 
          </Setter.Value> 
         </Setter> 
         <Setter TargetName="ExpandPath" 
           Property="Fill" 
           Value="#FF595959" /> 
         <Setter TargetName="ExpandPath" 
           Property="Stroke" 
           Value="#FF262626" /> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<!-- TreeViewItem Style --> 
<Style x:Key="{x:Type TreeViewItem}" 
     TargetType="{x:Type TreeViewItem}"> 
    <Setter Property="BorderThickness" 
      Value="1" /> 
    <Setter Property="Padding" 
      Value="2,0" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type TreeViewItem}"> 
       <Grid> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition MinWidth="19" 
              Width="Auto" /> 
         <ColumnDefinition Width="Auto" /> 
         <ColumnDefinition Width="*" /> 
        </Grid.ColumnDefinitions> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="Auto" /> 
         <RowDefinition /> 
        </Grid.RowDefinitions> 
        <ToggleButton x:Name="expander" 
            Style="{StaticResource ExpandCollapseToggleStyle}" 
            IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" 
            ClickMode="Press" /> 
        <Border x:Name="outerBorder" 
          Grid.Column="1" 
          BorderBrush="{TemplateBinding BorderBrush}" 
          BorderThickness="{TemplateBinding BorderThickness}" 
          CornerRadius="2" 
          SnapsToDevicePixels="true"> 
         <Border x:Name="innerBorder" 
           Background="{TemplateBinding Background}" 
           BorderThickness="1" 
           CornerRadius="1" 
           Padding="{TemplateBinding Padding}" 
           SnapsToDevicePixels="true"> 
          <ContentPresenter x:Name="PART_Header" 
               ContentSource="Header" 
               HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
               SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> 
         </Border> 
        </Border> 
        <ItemsPresenter x:Name="itemsHost" 
            Grid.Row="1" 
            Grid.Column="1" 
            Grid.ColumnSpan="2" /> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsExpanded" 
           Value="false"> 
         <Setter TargetName="itemsHost" 
           Property="Visibility" 
           Value="Collapsed" /> 
        </Trigger> 
        <Trigger Property="HasItems" 
           Value="false"> 
         <Setter TargetName="expander" 
           Property="Visibility" 
           Value="Hidden" /> 
        </Trigger> 
        <Trigger SourceName="outerBorder" 
           Property="IsMouseOver" 
           Value="true"> 
         <Setter TargetName="outerBorder" 
           Property="BorderBrush" 
           Value="{StaticResource HoverOuterBorderBrushKey}" /> 
         <Setter TargetName="innerBorder" 
           Property="Background" 
           Value="{StaticResource HoverBackgroundBrushKey}" /> 
         <Setter TargetName="innerBorder" 
           Property="BorderBrush" 
           Value="{StaticResource HoverInnerBorderBrushKey}" /> 
        </Trigger> 
        <Trigger Property="IsSelected" 
           Value="true"> 
         <Setter TargetName="outerBorder" 
           Property="BorderBrush" 
           Value="{StaticResource SelectedActiveOuterBorderBrushKey}" /> 
         <Setter TargetName="innerBorder" 
           Property="Background" 
           Value="{StaticResource SelectedActiveBackgroundBrushKey}" /> 
         <Setter TargetName="innerBorder" 
           Property="BorderBrush" 
           Value="{StaticResource SelectedActiveInnerBorderBrushKey}" /> 
        </Trigger> 
        <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="IsSelected" 
             Value="true" /> 
          <Condition Property="Selector.IsSelectionActive" 
             Value="false" /> 
         </MultiTrigger.Conditions> 
         <Setter TargetName="outerBorder" 
           Property="BorderBrush" 
           Value="{StaticResource SelectedInactiveOuterBorderBrushKey}" /> 
         <Setter TargetName="innerBorder" 
           Property="Background" 
           Value="{StaticResource SelectedInactiveBackgroundBrushKey}" /> 
         <Setter TargetName="innerBorder" 
           Property="BorderBrush" 
           Value="{StaticResource SelectedInactiveInnerBorderBrushKey}" /> 
        </MultiTrigger> 
        <Trigger Property="IsEnabled" 
           Value="false"> 
         <Setter Property="Foreground" 
           Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" /> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
+0

似乎看你的代码,这会工作。不过,我对微软感到有点失望,他们不支持在WPF的展示框架中选择默认的OS风格。这是否意味着我必须创建/覆盖所有控制器的模板,如DataGrid,TreeView等选择样式? – chrisva 2011-04-04 07:40:20

+0

@chrisva - 这是正确的,如果你想拥有一致的Windows 7外观,你还需要主题控制这些控件。 – CodeNaked 2011-04-04 17:39:36

+0

我想没有办法挂钩操作系统的活动主题或WPF的默认主题。要点是,同一个控件可以有N个控件。所以提供个人控制的主题是没有意义的。 – Mohanavel 2011-11-02 13:45:41