首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ComboBox DataTemplate中的WPF CheckBox在DataGrid中不起作用

ComboBox DataTemplate中的WPF CheckBox在DataGrid中不起作用
EN

Stack Overflow用户
提问于 2017-02-06 18:13:41
回答 1查看 361关注 0票数 1

我有一个ComboBox,它通过更改DataTemplate来包含一个CheckBox,当它自己完成时工作得很好,但是当它移动到DataGrid中时,CheckBox是不可点击的。

工作代码:

代码语言:javascript
运行
复制
<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添加复选标记,而不是选择任何内容。

非工作代码:

代码语言:javascript
运行
复制
<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>

EN

Stack Overflow用户

回答已采纳

发布于 2017-02-07 23:57:40

当你有一个嵌套的控件,它的父控件通常会劫持HitTestVisibility,你可以冒泡出来,允许嵌套控件响应它的正常事件,而不是它的父控件,方法是使用ClickMode枚举,通过添加到相关控件的ClickMode="Pressed"属性。

希望这能帮上忙,干杯!

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42065187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档