将SolidColorBrush绑定到ControlTemplate背景

时间:2016-01-20 08:41:42

标签: c# wpf user-interface binding controltemplate

您好我尝试将SolidColorBrush绑定到TabItem控件模板中的边框的Background属性。但是当我改变SolidColorBrush资源的值时,它表示"无法在对象上设置属性'#FF808080'因为它处于只读状态。"与没有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);
    }

错误:

1 个答案:

答案 0 :(得分:1)

请原谅我,但你做错了。而不是更改SolidColorBrush的属性,请转到整个资源项替换,如本文中的SO所述:see @Łukasz Rejman answer

例如:

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