当 ComboBox 位于 DataGridComboBoxColumn 内时,如何隐藏 ComboBox 的下拉列表以单击其中一个 ComboBoxItem-s?
How to hide the ComboBox's drop-down on click on one of its ComboBoxItem-s when the ComboBox is inside a DataGridComboBoxColumn?
我在下面有完整的 XAML 标记(不需要代码隐藏): Window
和 Grid
以及 DataGrid
的单列键入 DataGridComboBoxColumn
及其在 XAML
中设置的 Header
和 EditingElementStyle
属性。 EditingElementStyle
设置了 ComboBox
的 ItemContainerStyle
和 ItemsSource
。当我点击 ComboBoxItem
时,下拉菜单没有关闭,我需要它关闭。
我尝试删除 DataGridComboBoxColumn
元素的所有内容,下拉菜单仍然存在,但点击它时不会消失。
当我在下拉菜单外单击时,下拉菜单也会消失。
如果我把这个放在一个简单的 Window
:
<ComboBox>
<ComboBoxItem>a</ComboBoxItem>
<ComboBoxItem>b</ComboBoxItem>
<ComboBoxItem>c</ComboBoxItem>
</ComboBox>
有效。
XAML
<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>
预期:当我单击其中一个 ComboBoxItem
时,ComboBox
的下拉菜单关闭。实际结果:下拉菜单保持打开状态。
更新
我希望使用 ItemContainerStyle setter 因为我更改了一些基于绑定的 ComboBoxItem-s 的 FontStyle。这是一个更大项目的代码:
<ComboBoxItem FontStyle="{Binding Path=Style}" Content="{Binding Text}"/>
根据接受的答案,我使用此代码达到相同的效果,问题已解决:
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="ComboBoxItem">
<Setter Property="FontStyle" Value="{Binding Style}"/>
</Style>
</Setter.Value>
</Setter>
问题出在您的 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>
我在下面有完整的 XAML 标记(不需要代码隐藏): Window
和 Grid
以及 DataGrid
的单列键入 DataGridComboBoxColumn
及其在 XAML
中设置的 Header
和 EditingElementStyle
属性。 EditingElementStyle
设置了 ComboBox
的 ItemContainerStyle
和 ItemsSource
。当我点击 ComboBoxItem
时,下拉菜单没有关闭,我需要它关闭。
我尝试删除 DataGridComboBoxColumn
元素的所有内容,下拉菜单仍然存在,但点击它时不会消失。
当我在下拉菜单外单击时,下拉菜单也会消失。
如果我把这个放在一个简单的 Window
:
<ComboBox>
<ComboBoxItem>a</ComboBoxItem>
<ComboBoxItem>b</ComboBoxItem>
<ComboBoxItem>c</ComboBoxItem>
</ComboBox>
有效。
XAML
<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>
预期:当我单击其中一个 ComboBoxItem
时,ComboBox
的下拉菜单关闭。实际结果:下拉菜单保持打开状态。
更新
我希望使用 ItemContainerStyle setter 因为我更改了一些基于绑定的 ComboBoxItem-s 的 FontStyle。这是一个更大项目的代码:
<ComboBoxItem FontStyle="{Binding Path=Style}" Content="{Binding Text}"/>
根据接受的答案,我使用此代码达到相同的效果,问题已解决:
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="ComboBoxItem">
<Setter Property="FontStyle" Value="{Binding Style}"/>
</Style>
</Setter.Value>
</Setter>
问题出在您的 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>