编辑:颜色覆盖适用于我所拥有的一些按钮,其工作方式相同,除了我没有在主要样式资源中定义任何颜色,我只是设置颜色标签,但没有颜色属性
以下样式有3种颜色设置为整个样式中使用的默认颜色,但是如果我想在按钮上设置不同的颜色,我想要过度使用这3种颜色。
以下是ComboBox' s,第一个应该是默认颜色,第二个应该是绿色覆盖。
<ComboBox HorizontalAlignment="Left" SelectedIndex="0" Text="Select USB Device">
<ComboBoxItem Content="Test 1" />
<ComboBoxItem Content="Test 2" />
<ComboBoxItem Content="Test 3" />
</ComboBox>
<ComboBox HorizontalAlignment="Left" SelectedIndex="0" Text="Select USB Device" Style="{StaticResource ComboBlue}">
<ComboBoxItem Content="Test 1" />
<ComboBoxItem Content="Test 2" />
<ComboBoxItem Content="Test 3" />
</ComboBox>
XAML,主要风格
<!-- Combo Box Style -->
<Style TargetType="ComboBox">
<Style.Resources>
<SolidColorBrush x:Key="colour1" Color="{Binding Source={StaticResource NorGrey}, Path=Color}" />
<SolidColorBrush x:Key="colour2" Color="{Binding Source={StaticResource DarGrey}, Path=Color}" />
<SolidColorBrush x:Key="colour3" Color="{Binding Source={StaticResource LigGrey}, Path=Color}" />
<Style TargetType="ComboBoxItem">
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBoxItem">
<Border x:Name="Border"
Padding="4"
SnapsToDevicePixels="True"
Background="Transparent">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Border" Property="Background" Value="{StaticResource colour3}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ControlTemplate x:Key="ComboBoxToggleButton" TargetType="ToggleButton">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<Border x:Name="Border"
Grid.ColumnSpan="2"
CornerRadius="5"
Background="{StaticResource colour1}"/>
<Border Grid.Column="0"
CornerRadius="5,0,0,5"
Margin="1"
Background="{StaticResource colour2}" />
<Path x:Name="Arrow"
Grid.Column="1"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Data="M 0 0 L 4 4 L 8 0 Z"
Fill="White" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Border" Property="Background" Value="{StaticResource colour3}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<ControlTemplate x:Key="ComboBoxTextBox" TargetType="TextBox">
<Border x:Name="PART_ContentHost"
Focusable="False"
Background="{StaticResource colour1}" />
</ControlTemplate>
</Style.Resources>
<Setter Property="Margin" Value="5" />
<Setter Property="Foreground" Value="White" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.CanContentScroll" Value="True" />
<Setter Property="MinHeight" Value="20" />
<Setter Property="MinWidth" Value="80" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<Grid>
<ToggleButton x:Name="ToggleButton"
Template="{StaticResource ComboBoxToggleButton}"
Grid.Column="2"
Focusable="False"
ClickMode="Press"
IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" />
<ContentPresenter x:Name="ContentSite"
IsHitTestVisible="False"
Content="{TemplateBinding SelectionBoxItem}"
ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
Margin="6,6,10,6"
VerticalAlignment="Stretch"
HorizontalAlignment="Left" />
<TextBox x:Name="PART_EditableTextBox"
Style="{x:Null}"
Template="{StaticResource ComboBoxTextBox}"
HorizontalAlignment="Center"
VerticalAlignment="Bottom"
Margin="3,3,23,3"
Focusable="True"
Background="Transparent"
Visibility="Hidden"
IsReadOnly="{TemplateBinding IsReadOnly}" />
<Popup x:Name="Popup"
Placement="Bottom"
IsOpen="{TemplateBinding IsDropDownOpen}"
AllowsTransparency="True"
Focusable="False"
PopupAnimation="Slide">
<Grid x:Name="DropDown"
SnapsToDevicePixels="True"
MinWidth="{TemplateBinding ActualWidth}"
MaxHeight="{TemplateBinding MaxDropDownHeight}">
<Border x:Name="DropDownBorder"
Background="{StaticResource colour1}" />
<ScrollViewer Margin="4,6,4,6"
SnapsToDevicePixels="True">
<StackPanel IsItemsHost="True"
KeyboardNavigation.DirectionalNavigation="Contained" />
</ScrollViewer>
</Grid>
</Popup>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="HasItems" Value="False">
<Setter TargetName="DropDownBorder" Property="MinHeight" Value="95" />
</Trigger>
<Trigger Property="IsGrouping" Value="True">
<Setter Property="ScrollViewer.CanContentScroll" Value="True" />
</Trigger>
<Trigger SourceName="Popup" Property="AllowsTransparency" Value="True">
<Setter TargetName="DropDownBorder" Property="CornerRadius" Value="4" />
<Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
以下是用于覆盖颜色的样式,放置在主要样式之下。但由于某些原因,颜色没有变化。
<!-- Combo Box Colors -->
<Style BasedOn="{StaticResource {x:Type ComboBox}}" TargetType="ComboBox" x:Key="ComboGreen">
<Style.Resources>
<SolidColorBrush Color="{Binding Source={StaticResource NorGreen}, Path=Color}" x:Key="colour1" />
<SolidColorBrush Color="{Binding Source={StaticResource DarGreen}, Path=Color}" x:Key="colour2" />
<SolidColorBrush Color="{Binding Source={StaticResource LigGreen}, Path=Color}" x:Key="colour3" />
</Style.Resources>
</Style>
<Style BasedOn="{StaticResource {x:Type ComboBox}}" TargetType="ComboBox" x:Key="ComboGrey">
<Style.Resources>
<SolidColorBrush Color="{Binding Source={StaticResource NorGrey}, Path=Color}" x:Key="colour1" />
<SolidColorBrush Color="{Binding Source={StaticResource DarGrey}, Path=Color}" x:Key="colour2" />
<SolidColorBrush Color="{Binding Source={StaticResource LigGrey}, Path=Color}" x:Key="colour3" />
</Style.Resources>
</Style>
<Style BasedOn="{StaticResource {x:Type ComboBox}}" TargetType="ComboBox" x:Key="ComboBlue">
<Style.Resources>
<SolidColorBrush Color="{Binding Source={StaticResource NorBlue}, Path=Color}" x:Key="colour1" />
<SolidColorBrush Color="{Binding Source={StaticResource DarBlue}, Path=Color}" x:Key="colour2" />
<SolidColorBrush Color="{Binding Source={StaticResource LigBlue}, Path=Color}" x:Key="colour3" />
</Style.Resources>
</Style>
<Style BasedOn="{StaticResource {x:Type ComboBox}}" TargetType="ComboBox" x:Key="ComboRed">
<Style.Resources>
<SolidColorBrush Color="{Binding Source={StaticResource NorRed}, Path=Color}" x:Key="colour1" />
<SolidColorBrush Color="{Binding Source={StaticResource DarRed}, Path=Color}" x:Key="colour2" />
<SolidColorBrush Color="{Binding Source={StaticResource LigRed}, Path=Color}" x:Key="colour3" />
</Style.Resources>
</Style>
答案 0 :(得分:0)
我设法找到解决方案,我使用了错误的资源类型。在引用我应该使用DynamicResource
而不是StaticResource
的颜色时。