2017-03-02 40 views
3

在WPF項目中,我有一個ComboBox其中用於ItemTemplateDataTemplate改變基於該ComboBoxItem的綁定到Person對象的IsSelected財產Background顏色Border的。所以,在我的例子下面,當IsSelected=trueBackground=LightGreen組合框頭不尊重的ItemTemplate

ComboBox的下拉框打開時,這一切都很好。但是,如果在選擇Background=LightGreen的項目後關閉下拉菜單,ComboBox的標題不會顯示LightGreen顏色?

任何人都可以幫助找出我需要做什麼來顯示LightGreen顏色一旦ComboBox關閉了IsSelected=true項目?

非常感謝您的任何幫助。

下面是一些示例代碼來展示我的意思。

XAML:

<Window x:Class="combo.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:combo" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 

    <ComboBox ItemsSource="{Binding .}"> 
     <ComboBox.ItemTemplate> 
      <DataTemplate> 
       <Border HorizontalAlignment="Stretch"> 
        <Border.Style> 
         <Style TargetType="Border"> 
          <Style.Triggers> 
           <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=ComboBoxItem}, Path=DataContext.IsSelected}" Value="True"> 
            <Setter Property="Background" Value="LightGreen"/> 
           </DataTrigger> 
          </Style.Triggers> 
         </Style> 
        </Border.Style> 
        <StackPanel HorizontalAlignment="Stretch"> 
         <TextBlock Text="{Binding Name}"/> 
         <TextBlock Text="{Binding Email}"> 
         </TextBlock> 
        </StackPanel> 
       </Border> 
      </DataTemplate> 
     </ComboBox.ItemTemplate> 
    </ComboBox> 

</Grid> 

後面的代碼:

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 

     this.DataContext = new Person[] 
     { 
      new Person() { Name = "Mickey" , Email= "[email protected]" , IsSelected = false}, 
      new Person() { Name = "Donald" , Email= "[email protected]", IsSelected = true }, 
      new Person() { Name = "Pluto" , Email= "[email protected]", IsSelected = false } 
     }; 
    } 
} 

public class Person 
{ 
    public string Name { get; set; } 
    public string Email { get; set; } 
    public bool IsSelected { get; set; } 
} 

回答

2

在你觸發的RelativeSource尋找一個ComboBoxItem,你會發現只有在ItemsPresenter彈出內ComboBox

ItemsPres

當彈出被關閉,我們看到的是一個ToggleButtonContentPresenter

ContentPres

如果標記沒有給它扔掉:

<Style TargetType="Border"> 
    <Style.Triggers> 
     <DataTrigger 
      Binding="{Binding Path=Content.IsSelected, RelativeSource={RelativeSource 
         AncestorType=ContentPresenter}}" Value="True"> 
      <Setter Property="Background" Value="LightGreen"/> 
     </DataTrigger> 
    </Style.Triggers> 
</Style> 
+0

Hi @ bab7lon,謝謝你的回覆。它現在有效! – Cleve

0

我不確定,如果你在談論關閉時,組合框的樣子,或者如果你是談論下拉項目背景顏色不更新。

This answer已經提供了第一個解決方案,但如果你想知道爲什麼背景顏色上沒有選擇的項目更新,那是因爲你沒有約束力ComboBoxItem.IsSelectedPerson.IsSelected任何地方,所以他們不同步。

這裏有一個如何做補充,結合一個例子:

<ComboBox.Resources> 
    <Style TargetType="{x:Type ComboBoxItem}" > 
     <Setter Property="IsSelected" Value="{Binding IsSelected}" /> 
    </Style> 
</ComboBox.Resources> 

這就是說,你可能仍然有問題,因爲你不設置所選項目的默認值。想提供單一選擇capabilitilikethis通常情況下,我看到這個做更多的

ObservableCollection<Person> People { get; set; } 
Person SelectedPerson { get; set; } 

風格與XAML是

<ComboBox ItemsSource="{Binding People}" SelectedItem="{Binding SelectedPerson}" /> 

這樣,你不需要編寫任何代碼同步到將ComboBoxItem.IsSelected綁定到您的Person.IsSelected

+0

嗨@Rachel,謝謝你的回覆。這是我所說的第一個選擇,所以對於任何混淆都表示歉意。我也應該叫別人的Person對象的IsSelected屬性,因爲我認爲這可能是混淆的根源。乾杯。 – Cleve