将 SolidColorBrush 绑定到 ControlTemplate 背景

Binding SolidColorBrush to ControlTemplate Background

您好,我尝试将 SolidColorBrush 绑定到 TabItem 控件模板中边框的背景 属性。但是,当我更改 SolidColorBrush Resource 的值时,它说 "Cannot set a property on object '#FF808080' because it is in a read-only state." 与没有 ControlTemplate 的库存按钮一样,它工作得很好。

这是我的代码。 (XAML)

<Window.Resources>
    <SolidColorBrush x:Key="SolidColorBrush2" Color="Gray"/>
    <SolidColorBrush x:Key="SolidColorBrush3" Color="Black"/>

    <Style x:Key="TabItemTemplate" TargetType="TabItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TabItem">
                    <Border Name="Tab" Height="30" Padding="10 0" Margin="0 0 10 0">
                        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White">
                            <ContentPresenter x:Name="TabItemContent"  HorizontalAlignment="Center" VerticalAlignment="Center" ContentSource="Header"></ContentPresenter>
                        </TextBlock>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter TargetName="Tab" Property="Background" Value="{DynamicResource SolidColorBrush3}"/>
                        </Trigger>
                        <Trigger Property="IsSelected" Value="False">
                            <Setter TargetName="Tab" Property="Background" Value="{DynamicResource SolidColorBrush2}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <TabControl Name="TabControlRepaymentWorkflowInner" Grid.Row="0">
        <TabItem Header="ABC" Style="{DynamicResource TabItemTemplate}" Foreground="White">
            <StackPanel Orientation="Vertical" Margin="0 0 5 0" HorizontalAlignment="Center">
                <TextBlock Margin="5 0 0 0" Text="Enter note for Front Office"/>
                <Button Background="{DynamicResource SolidColorBrush2}" Content="Button1"/>
            </StackPanel>
        </TabItem>
        <TabItem Header="DEF" Style="{DynamicResource TabItemTemplate}" Foreground="White">
            <StackPanel Orientation="Vertical" Margin="0 0 5 0" HorizontalAlignment="Center">
                <TextBlock Margin="5 0 0 0" Text="Enter note for Front Office"/>
                <Button Background="{DynamicResource SolidColorBrush2}" Content="Button2"/>
            </StackPanel>
        </TabItem>
        <TabItem Header="GHI" Style="{DynamicResource TabItemTemplate}" Foreground="White">
            <StackPanel Orientation="Vertical" Margin="0 0 5 0" HorizontalAlignment="Center">
                <TextBlock Margin="5 0 0 0" Text="Enter note for Front Office"/>
                <Button Background="{DynamicResource SolidColorBrush2}" Content="Button3"/>
            </StackPanel>
        </TabItem>
    </TabControl>
    <Button Grid.Row="1" HorizontalAlignment="Center" Height="30" VerticalAlignment="Top" Click="ChangeTheme_Click">Change Theme</Button>
</Grid>

这是我的 C# 代码

private void ChangeTheme_Click(object sender, RoutedEventArgs e)
    {
        SolidColorBrush brush2 = (SolidColorBrush)Resources["SolidColorBrush2"];
        SolidColorBrush brush3 = (SolidColorBrush)Resources["SolidColorBrush3"];
        brush2.Color = Color.FromArgb(255, 200, 200, 200);
        brush3.Color = Color.FromArgb(255, 251, 132, 61);
    }

错误:

对不起,您的主题设置不正确。不要更改 SolidColorBrush 的属性,而是进行整个资源项替换,如 post 此处在 SO 上所述:

例如:

Resources["SolidColorBrush2"] = new SolidColorBrush(Color.FromArgb(255, 200, 200, 200));
Resources["SolidColorBrush3"] = new SolidColorBrush(Color.FromArgb(255, 251, 132, 61));