20

我有一个使用ListBoxaListBoxItem作为其容器的方法。问题是这似乎正在消耗事件(确切地说是部分事件),如果我点击我永远不会得到(但如果你点击它本身它会起作用)。DataTemplateExpanderExpanderClickHeaderSiteExpanderSelectedItemExpanderListBoxItem

关于如何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>

截图是预先编辑的

单击ListBoxItem结果 a SelectedItem

点击 <code>ListBoxItem</code> 得到一个 <code>SelectedItem</code>

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

单击 Expander 导致没有 <code>SelectedItem</code> 更新

4

2 回答 2

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>
于 2013-01-25T13:27:14.093 回答
7

那么下面的代码似乎与缺点(或者可能是优点)一起工作,每次只有选定的项目会被扩展。

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

IsHitTestVisible="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}"
IsExpanded="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}"

通过绑定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>

另一个带有代码的解决方案是这样的:

            <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>

和后面的代码:

    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;
            }
        }            
    }
于 2013-01-25T13:04:44.563 回答