Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

c# - Binding SolidColorBrush to ControlTemplate Background

Hi i tried to bind SolidColorBrush to the Background property of border in Control Template For TabItem. But When im changing the value of SolidColorBrush Resource it Says "Cannot set a property on object '#FF808080' because it is in a read-only state." Where as with stock button without ControlTemplate it works perfectly.

Here's my code. (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>

Here's my C# Code

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);
    }

And the Error:

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Pardon me but you're doing theming incorrectly. Instead of changing SolidColorBrush's properties, go for entire resource item replacement, as described in this post here on SO: see @?ukasz Rejman answer

For example:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...