将颜色附加为 DynamicResource 时 WPF TabControl 样式崩溃
WPF TabControl style crashes when attaching colors as DynamicResource
我正在尝试将类似 azure 的样式应用到我的 WPF TabControl,如 here 所述。
我想实现这样的外观:
<UserControl.Resources>
<ResourceDictionary>
<SolidColorBrush x:Key="AccentColorBrush" Color="CornflowerBlue" />
<SolidColorBrush x:Key="BlackColor" Color="Black" />
<SolidColorBrush x:Key="WhiteColor" Color="White" />
<SolidColorBrush x:Key="Gray5" Color="LightGray" />
<Style x:Key="AzureTabItem" TargetType="{x:Type TabItem}">
<Setter Property="BorderBrush" Value="{DynamicResource AccentColorBrush}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
<StackPanel>
<Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}"
Padding="12,2,24,2"
BorderThickness="0,0,0,4"
Margin="2,2,2,0"
SnapsToDevicePixels="True">
<Border.Style>
<Style TargetType="Border">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="{DynamicResource Gray5}"/>
</Setter.Value>
</Setter>
</Style>
</Border.Style>
<Label x:Name="root" FontSize="15">
<Label.Style>
<Style TargetType="Label">
<Setter Property="Foreground">
<Setter.Value>
<SolidColorBrush Color="{DynamicResource BlackColor}" />
</Setter.Value>
</Setter>
</Style>
</Label.Style>
<ContentPresenter ContentSource="Header" RecognizesAccessKey="True" />
</Label>
</Border>
<Canvas Height="10" Width="20" x:Name="arrow" Margin="25,0,0,0"
HorizontalAlignment="Left" SnapsToDevicePixels="True">
<Path Data="M 0 0 H 20 L 10 10 Z"
StrokeThickness="0"
Fill="{DynamicResource AccentColorBrush}"/>
</Canvas>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="root" Property="Foreground">
<Setter.Value>
<SolidColorBrush Color="{DynamicResource WhiteColor}" />
</Setter.Value>
</Setter>
<Setter TargetName="border" Property="Background">
<Setter.Value>
<SolidColorBrush Color="{DynamicResource AccentColor}"/>
</Setter.Value>
</Setter>
<Setter TargetName="border" Property="BorderBrush">
<Setter.Value>
<SolidColorBrush Color="{DynamicResource AccentColor}"/>
</Setter.Value>
</Setter>
<Setter TargetName="arrow" Property="Visibility" Value="Visible"/>
</Trigger>
<Trigger Property="IsSelected" Value="false">
<Setter TargetName="root" Property="Foreground">
<Setter.Value>
<SolidColorBrush Color="{DynamicResource BlackColor}" />
</Setter.Value>
</Setter>
<Setter TargetName="arrow" Property="Visibility" Value="Hidden"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="root" Property="Foreground">
<Setter.Value>
<SolidColorBrush Color="{DynamicResource WhiteColor}" />
</Setter.Value>
</Setter>
<Setter TargetName="border" Property="BorderBrush" Value="{DynamicResource AccentColorBrush}"></Setter>
<Setter TargetName="border" Property="Background">
<Setter.Value>
<SolidColorBrush Color="{DynamicResource AccentColor}"/>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</UserControl.Resources>
我遇到的问题是,当我通过 DynamicResource 引用 SolidColorBrush 资源(AccentColorBrush、BlackColor、WhiteColor 和 Gray5)时,它们无法正常工作。
如果我直接在它们正在工作的样式中应用颜色。
使用 DynamicResource 导致设计时异常和 xaml 视图崩溃,例如,对于 Gray5 资源来说:
InvalidOperationException: '#FFD3D3D3' is not a valid value for
property 'Color'.
然后我将此样式应用到我的 TabControl:
<TabControl Grid.Row="1"
Grid.RowSpan="5"
Grid.Column="0"
Grid.ColumnSpan="5">
<TabControl.Resources>
<Style TargetType="TabItem" BasedOn="{StaticResource AzureTabItem}"/>
</TabControl.Resources>
<TabItem Header="Home">
<!-- cotent here -->
</TabItem>
<!-- some other TabItems here -->
</TabControl>
您将资源定义为 SolidColorBrush
,但您正试图将它们用作 Color
。
<SolidColorBrush Color="{DynamicResource Gray5}"/>
SolidColorBrush.Color
需要一个Color
。 Gray5
是一个 SolidColorBrush
直接使用 Brush
资源:
<Setter Property="Background" Value="{DynamicResource Gray5}">
...而不是像这样创建另一个画笔:
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="{DynamicResource Gray5}"/>
</Setter.Value>
</Setter>
如果您使用后一种方法,您应该将资源定义为 Color
:
<Color x:Key="Gray5">LightGray</Color>
我正在尝试将类似 azure 的样式应用到我的 WPF TabControl,如 here 所述。
我想实现这样的外观:
<UserControl.Resources>
<ResourceDictionary>
<SolidColorBrush x:Key="AccentColorBrush" Color="CornflowerBlue" />
<SolidColorBrush x:Key="BlackColor" Color="Black" />
<SolidColorBrush x:Key="WhiteColor" Color="White" />
<SolidColorBrush x:Key="Gray5" Color="LightGray" />
<Style x:Key="AzureTabItem" TargetType="{x:Type TabItem}">
<Setter Property="BorderBrush" Value="{DynamicResource AccentColorBrush}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
<StackPanel>
<Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}"
Padding="12,2,24,2"
BorderThickness="0,0,0,4"
Margin="2,2,2,0"
SnapsToDevicePixels="True">
<Border.Style>
<Style TargetType="Border">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="{DynamicResource Gray5}"/>
</Setter.Value>
</Setter>
</Style>
</Border.Style>
<Label x:Name="root" FontSize="15">
<Label.Style>
<Style TargetType="Label">
<Setter Property="Foreground">
<Setter.Value>
<SolidColorBrush Color="{DynamicResource BlackColor}" />
</Setter.Value>
</Setter>
</Style>
</Label.Style>
<ContentPresenter ContentSource="Header" RecognizesAccessKey="True" />
</Label>
</Border>
<Canvas Height="10" Width="20" x:Name="arrow" Margin="25,0,0,0"
HorizontalAlignment="Left" SnapsToDevicePixels="True">
<Path Data="M 0 0 H 20 L 10 10 Z"
StrokeThickness="0"
Fill="{DynamicResource AccentColorBrush}"/>
</Canvas>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="root" Property="Foreground">
<Setter.Value>
<SolidColorBrush Color="{DynamicResource WhiteColor}" />
</Setter.Value>
</Setter>
<Setter TargetName="border" Property="Background">
<Setter.Value>
<SolidColorBrush Color="{DynamicResource AccentColor}"/>
</Setter.Value>
</Setter>
<Setter TargetName="border" Property="BorderBrush">
<Setter.Value>
<SolidColorBrush Color="{DynamicResource AccentColor}"/>
</Setter.Value>
</Setter>
<Setter TargetName="arrow" Property="Visibility" Value="Visible"/>
</Trigger>
<Trigger Property="IsSelected" Value="false">
<Setter TargetName="root" Property="Foreground">
<Setter.Value>
<SolidColorBrush Color="{DynamicResource BlackColor}" />
</Setter.Value>
</Setter>
<Setter TargetName="arrow" Property="Visibility" Value="Hidden"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="root" Property="Foreground">
<Setter.Value>
<SolidColorBrush Color="{DynamicResource WhiteColor}" />
</Setter.Value>
</Setter>
<Setter TargetName="border" Property="BorderBrush" Value="{DynamicResource AccentColorBrush}"></Setter>
<Setter TargetName="border" Property="Background">
<Setter.Value>
<SolidColorBrush Color="{DynamicResource AccentColor}"/>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</UserControl.Resources>
我遇到的问题是,当我通过 DynamicResource 引用 SolidColorBrush 资源(AccentColorBrush、BlackColor、WhiteColor 和 Gray5)时,它们无法正常工作。 如果我直接在它们正在工作的样式中应用颜色。
使用 DynamicResource 导致设计时异常和 xaml 视图崩溃,例如,对于 Gray5 资源来说:
InvalidOperationException: '#FFD3D3D3' is not a valid value for property 'Color'.
然后我将此样式应用到我的 TabControl:
<TabControl Grid.Row="1"
Grid.RowSpan="5"
Grid.Column="0"
Grid.ColumnSpan="5">
<TabControl.Resources>
<Style TargetType="TabItem" BasedOn="{StaticResource AzureTabItem}"/>
</TabControl.Resources>
<TabItem Header="Home">
<!-- cotent here -->
</TabItem>
<!-- some other TabItems here -->
</TabControl>
您将资源定义为 SolidColorBrush
,但您正试图将它们用作 Color
。
<SolidColorBrush Color="{DynamicResource Gray5}"/>
SolidColorBrush.Color
需要一个Color
。 Gray5
是一个 SolidColorBrush
直接使用 Brush
资源:
<Setter Property="Background" Value="{DynamicResource Gray5}">
...而不是像这样创建另一个画笔:
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="{DynamicResource Gray5}"/>
</Setter.Value>
</Setter>
如果您使用后一种方法,您应该将资源定义为 Color
:
<Color x:Key="Gray5">LightGray</Color>