2010-12-03 112 views
0

我试图在SelectedIndex在XAML中为-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 2010-12-03 17:07:09

2

您的TargetType和触发器属性是错误的。尝试以下,它确实有效。当您更改选择时,组合框将从黄色开始变化。

<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> 
相关问题