2017-08-17 106 views
-1

我注意到TextElement.Foreground不工作,但Background = Red和FontWeight = Bold工作正常。任何想法爲什麼前景不適用自定義樣式?如何自定義組合框內的複選框文本?

<ComboBox.ItemTemplate> 
    <DataTemplate> 
     <StackPanel Orientation="Horizontal"> 
      <CheckBox Name="ChkDayResource" Style="{DynamicResource CheckBoxBlueStyle}" 
          IsChecked="{Binding Path=IsSelected}" 
          Tag="{Binding Path=., RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}, AncestorLevel=1}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
          Click="ChkDayResource_Click" Content="{Binding Path=DayName}"> 
      </CheckBox> 
      <!--<TextBlock Width="Auto" Text="{Binding Path=DayName}" IsHitTestVisible="True"/>--> 
     </StackPanel> 
    </DataTemplate> 
</ComboBox.ItemTemplate> 
<ComboBox.ItemContainerStyle> 
    <Style TargetType="{x:Type ComboBoxItem}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ComboBoxItem}"> 
        <Border x:Name="Bd" 
         BorderBrush="{TemplateBinding BorderBrush}" 
         BorderThickness="0" 
         Margin="-1,0,-1,0" 
         Background="{TemplateBinding Background}"> 
         <StackPanel Orientation="Horizontal" Margin="10,0,10,0"> 
          <ContentPresenter x:Name="content" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
          VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /> 
         </StackPanel> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsHighlighted" Value="True"> 
          <Setter TargetName="Bd" Property="Background" Value="Red" /> 
          <Setter Property="TextElement.Foreground" Value="Yellow" /> 
          <Setter Property="TextElement.FontWeight" TargetName="content" Value="Bold" /> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</ComboBox.ItemContainerStyle> 

enter image description here

它是由於CheckBoxBlueStyle我申請的複選框?如果是的話,我該如何重寫樣式?

+0

PLS忽略給定的代碼和屏幕截圖之間的不同。背景和fontweight工作正常。 – soniality

+0

您是否缺少Setter中的TargetName屬性... –

回答

0

默認CheckBoxStyle有問題。檢查它例如在混合顯示:

<Style x:Key="CheckBoxStyle1" TargetType="{x:Type CheckBox}"> 
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
    <!-- other property setters --> 
</Style> 

所以,你需要直接或通過風格分配一個新Foreground,如果你不想要的默認值。

可能的解決方案:

<CheckBox Name="ChkDayResource" Foreground="{Binding Path=(TextElement.Foreground),RelativeSource={RelativeSource AncestorType=StackPanel}}"