扩展名为ListBoxItem不会触发选择

Mav*_*rik 18 wpf xaml

我有一个ListBoxListBoxItem使用DataTemplate,使用Expander作为容器.问题是,Expander似乎正在吃掉Click事件(确切地说是HeaderSite一部分Expander),SelectedItem如果我点击Expander它,我就永远不会得到(但是如果你点击它ListBoxItem自己就行了).

关于如何Expander与之搭配的任何想法ListBox

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

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

<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>
Run Code Online (Sandbox Code Playgroud)

屏幕截图是预编辑的

点击ListBoxItem结果SelectedItem:

单击<code>SelectedItem</code>更新(点击在Expander 1上,如虚线轮廓所示):</p>

<p><img rel=

LPL*_*LPL 12

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

<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>
Run Code Online (Sandbox Code Playgroud)


ilt*_*rtz 7

以下代码似乎与缺点(或可能是优势)一起工作,每次只有选定项目将被删除.

将以下2个属性应用于您的2 Expander

IsHitTestVisible="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}"
IsExpanded="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}"
Run Code Online (Sandbox Code Playgroud)

通过绑定IsHitTestVisible,它允许包含在其中的元素Expander与之交互.

导致:

            <ListBox x:Name="ListBox" IsSynchronizedWithCurrentItem="True">
                <ListBoxItem>
                    <Expander Header="Expandable Stuff 1" IsHitTestVisible="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}" IsExpanded="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}">
                        1
                    </Expander>
                </ListBoxItem>
                <ListBoxItem>
            <Expander Header="Expandable Stuff 2" IsHitTestVisible="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}" IsExpanded="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}">
                        2
                    </Expander>
                </ListBoxItem>
            </ListBox>
Run Code Online (Sandbox Code Playgroud)

代码背后的另一个解决方案是这样的:

            <ListBox x:Name="ListBox" IsSynchronizedWithCurrentItem="True">
                <ListBoxItem>
                    <Expander Header="Expandable Stuff 1" IsHitTestVisible="False" IsExpanded="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}">
                <StackPanel IsHitTestVisible="True">
                    <Label Content="1"/>
                </StackPanel>
            </Expander>
                </ListBoxItem>
                <ListBoxItem>
            <Expander Header="Expandable Stuff 2" ButtonBase.Click="Expander_Click_1" Tag="{Binding  RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}}">
                2                     
            </Expander>
                </ListBoxItem>
            </ListBox>
Run Code Online (Sandbox Code Playgroud)

和代码背后:

    private void Expander_Click_1(object sender, RoutedEventArgs e)
    {
        if (sender is Expander)
        {
            Expander senderExp = (Expander)sender;
            object obj = senderExp.Tag;
            if (obj is ListBoxItem)
            {
                ((ListBoxItem)obj).IsSelected = true;
            }
        }            
    }
Run Code Online (Sandbox Code Playgroud)

  • @iltzortz您的原始版本要好得多(没有代码隐藏!),最重要的是如果您将`IsHitTestVisible`绑定到`IsSelected`也是如此!你介意我用这个解决方案编辑还是你想做? (2认同)