当ComboBox位于DataGridComboBoxColumn内部时,如何在单击其ComboBoxItem-s之一时隐藏ComboBox的下拉菜单?

时间:2019-05-06 12:17:56

标签: c# wpf combobox datagrid datagridcomboboxcolumn

我在下面有完整的XAML标记(不需要任何代码隐藏):一个带有Window的{​​{1}}和带有一个Grid类型的一列的DataGridDataGridComboBoxColumn中设置的HeaderEditingElementStyle属性。 XAML设置EditingElementStyle中的ItemContainerStyleItemsSource。当我点击ComboBox时,下拉菜单不会关闭,我需要关闭它。

我尝试删除ComboBoxItem元素的所有内容,并且下拉列表在那里,为空,但单击时不会消失。

当我在下拉列表外部单击时,下拉列表也会消失。

如果我只是将其放在简单的DataGridComboBoxColumn中:

Window

有效。

XAML

<ComboBox>
    <ComboBoxItem>a</ComboBoxItem>
    <ComboBoxItem>b</ComboBoxItem>
    <ComboBoxItem>c</ComboBoxItem>
</ComboBox>

预期:当我单击<Window x:Class="cs_wpf_test_11.TestWindow" 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:cs_wpf_test_11" xmlns:sys="clr-namespace:System;assembly=mscorlib" mc:Ignorable="d" Title="TestWindow" Height="450" Width="800"> <Grid> <DataGrid AutoGenerateColumns="False"> <DataGrid.ItemsSource> <x:Array Type="{x:Type sys:String}"> <sys:String>dummy 1</sys:String> <sys:String>dummy 2</sys:String> </x:Array> </DataGrid.ItemsSource> <DataGrid.Columns> <DataGridComboBoxColumn Header="My only column"> <DataGridComboBoxColumn.EditingElementStyle> <Style TargetType="ComboBox"> <Setter Property="ItemContainerStyle"> <Setter.Value> <Style TargetType="ComboBoxItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <ComboBoxItem Content="{Binding}"/> </ControlTemplate> </Setter.Value> </Setter> </Style> </Setter.Value> </Setter> <Setter Property="ItemsSource"> <Setter.Value> <x:Array Type="{x:Type sys:String}"> <sys:String>test 1</sys:String> <sys:String>test 2</sys:String> <sys:String>test 3</sys:String> <sys:String>test 4</sys:String> </x:Array> </Setter.Value> </Setter> </Style> </DataGridComboBoxColumn.EditingElementStyle> </DataGridComboBoxColumn> </DataGrid.Columns> </DataGrid> </Grid> </Window> 之一时,ComboBox的下拉菜单关闭。实际结果:下拉菜单保持打开状态。

更新

我希望使用ItemContainerStyle设置程序,因为我基于Binding更改了某些ComboBoxItem-s的FontStyle。这是来自更大项目的代码:

ComboBoxItem

基于接受的答案,我使用此代码以达到相同的效果,问题得以解决:

<ComboBoxItem FontStyle="{Binding Path=Style}" Content="{Binding Text}"/>

1 个答案:

答案 0 :(得分:1)

问题是您的ItemContainerStyle。删除该部分,它会按预期工作。

这是默认的ItemContainerStyle组成的内容。您可以将其用作基础,但是如果创建一个新的空白容器,则将全部删除。

<ControlTemplate TargetType="{x:Type ComboBoxItem}">
    <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="TextElement.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
        </Trigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsSelected" Value="False"/>
                <Condition Property="IsMouseOver" Value="True"/>
                <Condition Property="IsKeyboardFocused" Value="False"/>
            </MultiTrigger.Conditions>
            <Setter Property="Background" TargetName="Bd" Value="{StaticResource ComboBoxItem.ItemsviewHover.Background}"/>
            <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource ComboBoxItem.ItemsviewHover.Border}"/>
        </MultiTrigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsSelected" Value="True"/>
                <Condition Property="IsMouseOver" Value="False"/>
                <Condition Property="IsKeyboardFocused" Value="True"/>
            </MultiTrigger.Conditions>
            <Setter Property="Background" TargetName="Bd" Value="{StaticResource ComboBoxItem.ItemsviewSelected.Background}"/>
            <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource ComboBoxItem.ItemsviewSelected.Border}"/>
        </MultiTrigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsSelected" Value="True"/>
                <Condition Property="IsMouseOver" Value="True"/>
            </MultiTrigger.Conditions>
            <Setter Property="Background" TargetName="Bd" Value="{StaticResource ComboBoxItem.ItemsviewSelectedHover.Background}"/>
            <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource ComboBoxItem.ItemsviewSelectedHover.Border}"/>
        </MultiTrigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsSelected" Value="True"/>
                <Condition Property="IsMouseOver" Value="False"/>
                <Condition Property="IsKeyboardFocused" Value="False"/>
            </MultiTrigger.Conditions>
            <Setter Property="Background" TargetName="Bd" Value="{StaticResource ComboBoxItem.ItemsviewSelectedNoFocus.Background}"/>
            <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource ComboBoxItem.ItemsviewSelectedNoFocus.Border}"/>
        </MultiTrigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsSelected" Value="False"/>
                <Condition Property="IsMouseOver" Value="False"/>
                <Condition Property="IsKeyboardFocused" Value="True"/>
            </MultiTrigger.Conditions>
            <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource ComboBoxItem.ItemsviewFocus.Border}"/>
        </MultiTrigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsSelected" Value="False"/>
                <Condition Property="IsMouseOver" Value="True"/>
                <Condition Property="IsKeyboardFocused" Value="True"/>
            </MultiTrigger.Conditions>
            <Setter Property="Background" TargetName="Bd" Value="{StaticResource ComboBoxItem.ItemsviewHoverFocus.Background}"/>
            <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource ComboBoxItem.ItemsviewHoverFocus.Border}"/>
        </MultiTrigger>
    </ControlTemplate.Triggers>
</ControlTemplate>