列表框展开所选项目

Posted

技术标签:

【中文标题】列表框展开所选项目【英文标题】:ListBox expand selected item 【发布时间】:2010-11-20 22:51:50 【问题描述】:

我有以下代码sn-p(复制粘贴到kaxaml、xamlpad等中试试) 折叠除所选项目之外的所有项目。但是,我想恢复到所有可见的 当鼠标不在ListBox 上时,我只是无法让它在没有代码的情况下工作。我正在使用IsMouseOver ListBox 属性来设置ListBox 上的选定项属性以尝试触发更新但没有运气。 有什么想法吗?

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid> <ListBox Name="lb" Width="100" Height="100" Background="Red" SelectionMode="Single"> <ListBox.ItemsPanel> <ItemsPanelTemplate> <UniformGrid Columns="1"/> </ItemsPanelTemplate> </ListBox.ItemsPanel> <ListBoxItem Background="AliceBlue">Item 1 </ListBoxItem> <ListBoxItem Background="Aquamarine">Item </ListBoxItem> <ListBoxItem Background="Azure">Item </ListBoxItem> <ListBox.ItemContainerStyle> <Style TargetType="x:Type ListBoxItem"> <Setter Property="Visibility" Value="Visible"/> <Style.Triggers> <MultiDataTrigger> <MultiDataTrigger.Conditions> <Condition Binding="Binding RelativeSource=RelativeSource Self, Path=IsSelected" Value="False"/> <Condition Binding="Binding RelativeSource=RelativeSource Mode=FindAncestor,AncestorType=x:Type ListBox,Path=SelectedItems.Count" Value="1"/> </MultiDataTrigger.Conditions> <MultiDataTrigger.EnterActions> <BeginStoryboard> <Storyboard Duration="0:0:1"> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility"> <DiscreteObjectKeyFrame KeyTime="0:0:1" Value="x:Static Visibility.Collapsed"/> </ObjectAnimationUsingKeyFrames> <DoubleAnimation Duration="0:0:1" Storyboard.TargetProperty="Opacity" To="0"/> </Storyboard> </BeginStoryboard> </MultiDataTrigger.EnterActions> </MultiDataTrigger> <MultiDataTrigger> <MultiDataTrigger.Conditions> <Condition Binding="Binding RelativeSource=RelativeSource Self, Path=IsSelected" Value="True"/> <Condition Binding="Binding RelativeSource=RelativeSource Mode=FindAncestor,AncestorType=x:Type ListBox,Path=SelectedItems.Count" Value="1"/> </MultiDataTrigger.Conditions> <MultiDataTrigger.EnterActions> <BeginStoryboard> <Storyboard Duration="0:0:0"> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility"> <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="x:Static Visibility.Visible"/> </ObjectAnimationUsingKeyFrames> <DoubleAnimation Duration="0:0:0" Storyboard.TargetProperty="Opacity" To="1"/> </Storyboard> </BeginStoryboard> </MultiDataTrigger.EnterActions> </MultiDataTrigger> </Style.Triggers> </Style> </ListBox.ItemContainerStyle> <ListBox.Style> <Style> <Style.Triggers> <Trigger Property="ListBox.IsMouseOver" Value="False"> <Setter Property="ListBox.SelectedItem" Value="x:Null"/> <Setter Property="ListBoxItem.IsSelected" Value="False"/> <Setter Property="ListBox.SelectedIndex" Value="-1"/> </Trigger> <EventTrigger RoutedEvent="Mouse.MouseLeave"> <BeginStoryboard> <Storyboard> <Int32Animation Duration="0:0:0" Storyboard.TargetProperty="SelectedIndex" To="-1"/> </Storyboard> </BeginStoryboard> </EventTrigger> </Style.Triggers> </Style> </ListBox.Style> </ListBox> </Grid> </Page>

【问题讨论】:

【参考方案1】:

将您的样式移动到资源并在鼠标悬停在 ListBox 上时应用它。`

<Page.Resources>
    <Style x:Key="CustomStyle" TargetType="x:Type ListBoxItem">
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="Binding RelativeSource=RelativeSource Self, Path=IsSelected" Value="False"/>
                    <Condition Binding="Binding RelativeSource=RelativeSource Mode=FindAncestor,AncestorType=x:Type ListBox,Path=SelectedItems.Count" Value="1"/>
                </MultiDataTrigger.Conditions>
                <MultiDataTrigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard Duration="0:0:1">
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility">
                                <DiscreteObjectKeyFrame KeyTime="0:0:1" Value="x:Static Visibility.Collapsed"/>
                            </ObjectAnimationUsingKeyFrames>
                            <DoubleAnimation Duration="0:0:1" Storyboard.TargetProperty="Opacity" To="0"/>
                        </Storyboard>
                    </BeginStoryboard>
                </MultiDataTrigger.EnterActions>
                <MultiDataTrigger.ExitActions>
                    <BeginStoryboard>
                        <Storyboard Duration="0:0:0">
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility">
                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="x:Static Visibility.Visible"/>
                            </ObjectAnimationUsingKeyFrames>
                            <DoubleAnimation Duration="0:0:0" Storyboard.TargetProperty="Opacity" To="1"/>
                        </Storyboard>
                    </BeginStoryboard>
                </MultiDataTrigger.ExitActions>
            </MultiDataTrigger>
        </Style.Triggers>
    </Style>
</Page.Resources>

<Grid>
    <ListBox
       Name="lb"
       Width="100"
       Height="100"
       Background="Red"
       SelectionMode="Single">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Columns="1"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBoxItem Background="AliceBlue">Item 1
        </ListBoxItem>
        <ListBoxItem Background="Aquamarine">Item
        </ListBoxItem>
        <ListBoxItem Background="Azure">Item
        </ListBoxItem>
        <ListBox.Style>
            <Style>
                <Style.Triggers>
                    <Trigger Property="ListBox.IsMouseOver" Value="True">
                        <Setter 
                            Property="ListBox.ItemContainerStyle" 
                            Value="StaticResource CustomStyle"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ListBox.Style>
    </ListBox>
</Grid>

` 还要注意 MultiDataTrigger.ExitActions 的用法,这些是在触发器对象变为非活动状态时应用的操作。

【讨论】:

我已经完成了这个问题,现在我明白了为什么它会这样工作:) 原因是动画在设置依赖属性时具有最高优先级。因此,当您使用动画设置了一些属性并且想要返回初始值时,请使用动画来执行此操作,就像在 MultiDataTrigger.ExitActions 元素中所做的那样。

以上是关于列表框展开所选项目的主要内容,如果未能解决你的问题,请参考以下文章

在 C# 中将所选项目从一个列表框移动到另一个列表框

单击列表框时,如何在文本框中以另一种形式从ms access 2010中的列表框中移动所选项目

从DblClick上的列表框导航到所选项目(excel)

复制并删除所选的列表框项目

所选列表框项的前景色的 Wpf 样式资源

从列表框中选择,从另一个列表框中选择填充