WPF - 覆盖类库中的系统控件样式

时间:2014-03-09 06:30:30

标签: c# wpf xaml

正如您所看到的,默认Aero主题中ListBoxItem的样式非常老式,所以我发现了一种新的样式,如果我把它放在MainWindow.Resources中就可以了。

但是如果我将它放在Themes/Aero.NormalColor.xaml的类库中并引用它(我也在该库中使用了一些自定义控件)但它不起作用。任何解决方法? :(

ThemeInfo用于类库:

[assembly: ThemeInfo(ResourceDictionaryLocation.SourceAssembly, ResourceDictionaryLocation.SourceAssembly)]

ThemeInfo主要项目:

[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]

Themes/Aero.NormalColor.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <!-- 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" Background="{TemplateBinding Control.Background}"
                                BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="2"
                                BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True">
                            <Border x:Name="innerBorder" Background="{TemplateBinding Background}"
                                    BorderThickness="1" CornerRadius="1" Padding="{TemplateBinding Padding}"
                                    SnapsToDevicePixels="True">
                                <ContentPresenter ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
                                                  ContentTemplateSelector=
                                                    "{TemplateBinding ContentControl.ContentTemplateSelector}"
                                                  ContentStringFormat=
                                                    "{TemplateBinding ContentControl.ContentStringFormat}"
                                                  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>

    <!-- 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>
</ResourceDictionary>

2 个答案:

答案 0 :(得分:0)

我想将这个'覆盖'应用于应用程序中的所有ListBoxItem,摆脱'x:Key =“{x:Type ListBoxItem}”'

答案 1 :(得分:0)

在包含列表框的Window.xaml文件中,您还必须合并该resourcedictionary。

<Window.Resources>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="clr-namespace: [namespace_from_your_referenced_library]">
    </ResourceDictionary.MergedDictionaries>
</Window.Resources>

此外,您可以合并来自库中的资源(.xaml.cs) 即在下一个代码中,我们合并了应用程序级别的资源

ResourceDictionary res = new ResourceDictionary();
res.Source = new Uri("LIBRARYNAME;fullclassname" + ".xaml", UriKind.Relative);

try{
    Application.Current.Resources.MergedDictionaries.Add(res);
}
catch {
    // may be resource was loaded ?
}