2012-10-17 2 views
1

WPF 응용 프로그램에서 항목의 스타일을 지정하지만 선택되는 항목에 따라 예기치 않은 동작이 발생하는 ComboBox가 있습니다.WPF ComboBox 스타일이 예상과 다른 이유는 무엇입니까?

다음 XAML 조각은 문제를 보여줍니다 :이 세 가지 항목 간단한 콤보 상자를 표시

<Page 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Page.Resources> 
    <Style x:Key="ItemStyle" TargetType="{x:Type ComboBoxItem}"> 
     <Style.Triggers> 
      <Trigger Property="Content" Value="ABC"> 
       <Setter Property="FontStyle" Value="Oblique"/> 
       <Setter Property="Foreground" Value="Red"/> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
    <Style x:Key="BoxStyle" TargetType="{x:Type ComboBox}"> 
     <Style.Triggers> 
      <Trigger Property="Text" Value="ABC"> 
       <Setter Property="FontStyle" Value="Italic"/> 
       <Setter Property="Foreground" Value="Red"/> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
    </Page.Resources> 
    <Border Width="200"> 
     <ComboBox Style="{StaticResource BoxStyle}" 
       ItemContainerStyle="{StaticResource ItemStyle}" 
       Height="20"> 
      <ComboBoxItem>ABC</ComboBoxItem> 
      <ComboBoxItem>DEF</ComboBoxItem> 
      <ComboBoxItem>GHI</ComboBoxItem> 
     </ComboBox> 
    </Border> 
</Page> 

을; ABC, DEF 및 GHI. ABC가 드롭 다운에 기울어 진 빨간색 텍스트와 함께 선택 상자가 선택되면 선택 상자에 표시됩니다.

그러나 다시 드롭 다운을 열면 3 개의 항목 모두가 기울어지고 빨간색으로 표시됩니다.

DEF 또는 GHI 항목을 선택한 경우 일반 글꼴로 표시되며 검은 색으로 표시되며 드롭 다운을 다시 열면 ABC가 여전히 기울어 진 빨간색으로 표시되어 올바르게 표시됩니다.

내가 뭘 잘못하고 있니?

답변

1

이것은 Dependency Property Value Precedence입니다. ABC를 선택하면 드롭 다운의 ComboBoxItemComboBox에서 FontStyleForeground을 상속합니다.

<Page.Resources> 
      <Style x:Key="ItemStyle" 
        TargetType="{x:Type ComboBoxItem}"> 
       <Setter Property="FontStyle" 
         Value="Normal" /> 
       <Setter Property="Foreground" 
         Value="Black" /> 
       <Style.Triggers> 
        <Trigger Property="Content" 
          Value="ABC"> 
         <Setter Property="FontStyle" 
           Value="Oblique" /> 
         <Setter Property="Foreground" 
           Value="Red" /> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
      <Style x:Key="BoxStyle" 
        TargetType="{x:Type ComboBox}"> 
       <Style.Triggers> 
        <Trigger Property="Text" 
          Value="ABC"> 
         <Setter Property="FontStyle" 
           Value="Italic" /> 
         <Setter Property="Foreground" 
           Value="Red" /> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
     </Page.Resources> 
     <Border Width="200"> 
      <ComboBox Style="{StaticResource BoxStyle}" 
         ItemContainerStyle="{StaticResource ItemStyle}" 
         Height="20"> 
       <ComboBoxItem>ABC</ComboBoxItem> 
       <ComboBoxItem>DEF</ComboBoxItem> 
       <ComboBoxItem>GHI</ComboBoxItem> 
      </ComboBox> 
     </Border> 
+0

좋아요, 문제를 해결 :

ComboBoxItem s는 콤보 상자에서의 fontStyle 및 전경을 상속하지 않으므로 코드를 수정합니다. 설명 해줘서 고마워. –