繁体   English   中英

ComboBox标头不尊重ItemTemplate

[英]ComboBox header not respecting ItemTemplate

在WPF项目中,我有一个ComboBox ,其中用于ItemTemplateDataTemplate根据ComboBoxItem绑定的Person对象的IsSelected属性更改BorderBackground颜色。 因此,在下面的示例中,当IsSelected=trueBackground=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; }
}

触发器中的RelativeSource查找ComboBoxItem ,您只能在弹出ComboBoxItemsPresenter找到它。

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>

我不确定你是否在谈论ComboBox在关闭时的外观,或者你是在谈论下拉项目背景颜色没有更新。

这个答案已经为第一个提供了解决方案,但如果您想知道为什么背景颜色没有在所选项目上更新,那是因为您没有将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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM