2010-12-03 13 views
0

XAMLでSelectedIndexが-1の場合、WPFコンボボックスの背景色を設定しようとしています。トリガーで背景色を設定しようとしましたが、トリガーのプロパティでバインドを設定できないというエラーが表示されます。WPF ComboBoxItemコンボボックスSelectedIndexが-1のときの背景の変更

おかげ

<ComboBox 
        x:Name="cbFormNameList" 
        ItemsSource="{Binding}" 
        DisplayMemberPath="Name" 
        SelectedValuePath="Name"> 
       <ComboBox.Style> 
        <Style TargetType="{x:Type ComboBoxItem}"> 
         <Style.Triggers> 
          <Trigger Property="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SelectedIndex}" Value="-1"> 
           <Setter Property="Background" Value="#FFFAFFA9"/> 
          </Trigger> 
         </Style.Triggers> 
        </Style> 
       </ComboBox.Style> 
      </ComboBox> 

答えて

2

あなたのスタイルは、ComboBoxItemはなく、コンボボックス自体をターゲットしています。

<ComboBox.Style> 
    <Style TargetType="{x:Type ComboBox}"> 
     <Style.Triggers> 
      <Trigger Property="SelectedIndex" Value="-1"> 
       <Setter Property="Background" Value="#FFFAFFA9"/> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</ComboBox.Style> 

スタイルは、直接コンボボックスをターゲットにする必要があります。何も選択されていない場合は、以下のコンボボックスの背景を変更します。

+0

すごく簡単でした。ありがとう – sondlerd

2

TargetTypeとTriggerプロパティが間違っています。以下を試してみてください。うまくいきます。コンボボックスは黄色で始まり、選択を変更すると変更されます。

<ComboBox> 
     <ComboBoxItem> 
      Test 
     </ComboBoxItem> 
     <ComboBox.Style> 
      <Style TargetType="ComboBox"> 
       <Style.Triggers> 
        <Trigger Property="SelectedIndex" Value="-1"> 
         <Setter Property="Background" Value="#FFFAFFA9"/> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
     </ComboBox.Style> 
    </ComboBox> 
関連する問題