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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…