【问题标题】:ComboBox header not respecting ItemTemplateComboBox 标题不尊重 ItemTemplate
【发布时间】:2017-03-02 16:57:07
【问题描述】:

在 WPF 项目中,我有一个 ComboBox,其中用于 ItemTemplateDataTemplate 根据 Person 对象的 IsSelected 属性更改 Background 颜色ComboBoxItem 是必然的。所以,在我下面的例子中,当IsSelected=true Background=LightGreen.

ComboBox 的下拉菜单打开时,这一切都很好。但是,在选择带有Background=LightGreen 的项目后关闭下拉菜单时,ComboBox 的标题不会显示LightGreen 颜色。

ComboBox 关闭IsSelected=true 项目后,我需要做什么才能显示LightGreen 颜色?

这里有一些示例代码来说明我的意思。

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>
</Window>

后面的代码:

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

        this.DataContext = new Person[]
        {
            new Person() { Name = "Mickey" , Email= "m@disney.com" , IsSelected = false},
            new Person() { Name = "Donald" , Email= "d@disney.com", IsSelected = true },
            new Person() { Name = "Pluto" , Email= "p@disney.com", IsSelected = false }
        };
    }
}

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

【问题讨论】:

    标签: c# wpf xaml combobox


    【解决方案1】:

    触发器中的RelativeSource 查找ComboBoxItem,您只能在ComboBox 弹出窗口中的ItemsPresenter 中找到它。

    当弹出窗口关闭时,我们看到的是ToggleButtonContentPresenter

    如果标记没有泄露:

    <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>
    

    【讨论】:

      【解决方案2】:

      我不确定您是在谈论 ComboBox 关闭时的外观,还是在谈论下拉项目背景颜色未更新。

      This answer 已经为第一个提供了解决方案,但是如果您想知道为什么所选项目的背景颜色没有更新,那是因为您没有将 ComboBoxItem.IsSelected 绑定到 Person.IsSelected 任何地方,所以他们不同步。

      这是一个如何添加该绑定的示例:

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

      也就是说,您可能仍然遇到问题,因为您没有设置默认选定项目。通常,当想要提供类似这样的单选功能时,我看到这样做更多的是

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

      使用 XAML

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

      这样,您无需编写任何同步代码即可将ComboBoxItem.IsSelected 绑定到您的Person.IsSelected

      【讨论】:

      • 嗨@Rachel,感谢您的回复。这是我所说的第一个选项,因此对任何混淆表示歉意。我还应该将我的Person 对象的IsSelected 属性称为其他东西,因为我认为这可能是造成混乱的根源。干杯。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 2021-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多