ComboBox DataTemplate 中的 WPF CheckBox 在 DataGrid 中不起作用
WPF CheckBox in ComboBox DataTemplate does not work in DataGrid
我有一个 ComboBox
,它通过更改 DataTemplate
包含一个 CheckBox
,单独完成时效果很好,但是当将它移到 DataGrid
中时CheckBox
不可点击。
工作代码:
<ComboBox ItemsSource="{Binding WrapUpHelper.WrapUps}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsSelected}"
Width="20" />
<TextBlock Text="{Binding FriendlyDescription}" />
</StackPanel>
<ListBox ItemsSource="{Binding WrapUps}"
Visibility="{Binding Path=IsSelected, Converter={StaticResource BooleanToVisibilityConverter}}"
BorderThickness="0"
Background="Transparent">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsSelected}"
Width="20" />
<TextBlock Text="{Binding FriendlyDescription}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
证明:
但是,当将相同的逻辑添加到 DataGrid
时,ComboBox
没有将复选标记添加到 CheckBox
,而是在没有选择任何内容的情况下关闭。
非工作代码:
<DataGridTemplateColumn Header="Wrap up" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding WrapUpHelper.WrapUps}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsSelected}"
Width="20" />
<TextBlock Text="{Binding FriendlyDescription}" />
</StackPanel>
<ListBox ItemsSource="{Binding WrapUps}"
Visibility="{Binding Path=IsSelected, Converter={StaticResource BooleanToVisibilityConverter}}"
BorderThickness="0"
Background="Transparent">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsSelected}" Width="20" />
<TextBlock Text="{Binding FriendlyDescription}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
当您有一个嵌套控件,其父控件通常会劫持 HitTestVisibility 时,您可以通过 [=16] 使用 ClickMode 枚举来冒泡并允许嵌套控件响应它的正常事件来代替它的父控件=] 的 ClickMode="Pressed"
被添加到相关控件中。
希望对您有所帮助,干杯!
我有一个 ComboBox
,它通过更改 DataTemplate
包含一个 CheckBox
,单独完成时效果很好,但是当将它移到 DataGrid
中时CheckBox
不可点击。
工作代码:
<ComboBox ItemsSource="{Binding WrapUpHelper.WrapUps}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsSelected}"
Width="20" />
<TextBlock Text="{Binding FriendlyDescription}" />
</StackPanel>
<ListBox ItemsSource="{Binding WrapUps}"
Visibility="{Binding Path=IsSelected, Converter={StaticResource BooleanToVisibilityConverter}}"
BorderThickness="0"
Background="Transparent">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsSelected}"
Width="20" />
<TextBlock Text="{Binding FriendlyDescription}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
证明:
但是,当将相同的逻辑添加到 DataGrid
时,ComboBox
没有将复选标记添加到 CheckBox
,而是在没有选择任何内容的情况下关闭。
非工作代码:
<DataGridTemplateColumn Header="Wrap up" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding WrapUpHelper.WrapUps}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsSelected}"
Width="20" />
<TextBlock Text="{Binding FriendlyDescription}" />
</StackPanel>
<ListBox ItemsSource="{Binding WrapUps}"
Visibility="{Binding Path=IsSelected, Converter={StaticResource BooleanToVisibilityConverter}}"
BorderThickness="0"
Background="Transparent">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsSelected}" Width="20" />
<TextBlock Text="{Binding FriendlyDescription}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
当您有一个嵌套控件,其父控件通常会劫持 HitTestVisibility 时,您可以通过 [=16] 使用 ClickMode 枚举来冒泡并允许嵌套控件响应它的正常事件来代替它的父控件=] 的 ClickMode="Pressed"
被添加到相关控件中。
希望对您有所帮助,干杯!