作为 ListBoxItem 的扩展器不会触发选择 [英] Expander as ListBoxItem doesn't trigger Selection

查看:20
本文介绍了作为 ListBoxItem 的扩展器不会触发选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListBoxListBoxItem 使用 DataTemplate 使用 Expander 作为其容器.问题是 Expander 似乎正在吃掉 Click 事件(准确地说是 HeaderSite Expander 的一部分)并且如果我点击 Expander,我永远不会得到 SelectedItem(但如果你点击 ListBoxItem 本身,它会起作用).

I've got a ListBox with ListBoxItem using a DataTemplate that uses Expander as its container. The problem is that Expander appears to be eating up Click event (HeaderSite part of Expander to be exact) and I never get the SelectedItem if I click on Expander (but it works if you click on ListBoxItem itself).

知道如何让 ExpanderListBox 完美配合吗?

Any idea on how to get Expander to play nicely with ListBox?

这是一个简化的 Xaml,可以重现问题(不需要背后的代码):

Here's a simplified Xaml that will reproduce the problem (no code behind needed):

编辑代码更新以更接近我的实际模板,但屏幕截图仍然来自以前的修订版(问题相同 - 这只是为了澄清第一个答案的问题)

Edit Code updated to bring closer to my actual template but screenshots are still from previous revision (the problem is same - this is just to clarify problem with first answer)

<Window x:Class="ListBoxSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TextBlock>
            <Run Text="Selected Item" />
            <Run Text="{Binding ElementName=ListBox, Path=SelectedItem}" />
        </TextBlock>
        <ListBox x:Name="ListBox">
            <ListBoxItem>
                <Expander Header="Expandable Stuff 1">
                    <ListBox>
                        <ListBoxItem>1.1</ListBoxItem>
                        <ListBoxItem>1.2</ListBoxItem>
                    </ListBox>
                </Expander>
            </ListBoxItem>
            <ListBoxItem>
                <Expander Header="Expandable Stuff 2">
                    <ListBox>
                        <ListBoxItem>2.1</ListBoxItem>
                        <ListBoxItem>2.2</ListBoxItem>
                    </ListBox>
                </Expander>
            </ListBoxItem>
        </ListBox>
    </StackPanel>
</Window>

屏幕截图已预先编辑

点击ListBoxItem 产生一个SelectedItem:

点击 Expander 导致 no SelectedItem 更新(点击是在 Expander 1 上,如虚线轮廓所示):

Clicking on Expander resulting in no SelectedItem update (click was on Expander 1 as evident by dashed outline):

推荐答案

没有代码,你也可以做到这一点

Without code behind you could do this

<ListBox.ItemContainerStyle>
    <Style>
        <Style.Triggers>
            <EventTrigger RoutedEvent="Control.PreviewMouseLeftButtonDown">
                <BeginStoryboard>
                    <Storyboard Storyboard.TargetProperty="(Selector.IsSelected)">
                        <BooleanAnimationUsingKeyFrames Duration="0:0:0">
                            <DiscreteBooleanKeyFrame Value="True" />
                        </BooleanAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Style.Triggers>
    </Style>
</ListBox.ItemContainerStyle>

这篇关于作为 ListBoxItem 的扩展器不会触发选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆