2016-01-20 71 views
3

您好,我試圖將SolidColorBrush綁定到TabItem控件模板中邊框的背景屬性。但是當即時更改SolidColorBrush資源的值時,它說「無法在對象'#FF808080'上設置屬性,因爲它處於只讀狀態。」與沒有ControlTemplate的股票按鈕一樣,它可以很好地工作。將SolidColorBrush綁定到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

請原諒我,但你在做主題化不正確。相反,改變的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)); 
+0

謝謝配合。 :)需要更換SolidColorBrush。 –

相關問題