【问题标题】:isselected on second listview same as on the first在第二个列表视图上被选中,与第一个相同
【发布时间】:2012-03-15 13:02:08
【问题描述】:

我有一个包含 2 个列表视图的网格。列表视图是相同的(只有 itemsource 与 c 的其他项目不同)datatemplate = Stackpanel 具有 1 个标签和另一个网格。现在我希望网格(在数据模板内的堆栈面板内)仅在选中项目(标签)时才可见。我用这段代码试了一下(我把它放在 Listview 的数据模板中:

<StackPanel>
<Label content={binding blabla} />
<Grid Visibility="{Binding IsSelected,RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}, Mode=FindAncestor}, Mode=OneWay,  Converter={StaticResource BooleanToVisibilityConverter}}" >
...random labels etc...
</Gird>
</StackPanel>

这行得通!但是,如果我在第二个列表视图中选择一个项目(并且只有第二个),那么第一个也显示该网格(在同一“项目级别”上)。(例如,在第二个列表视图中,我选择第三个项目(标签),然后网格显示在第二个列表视图的第三个项目上,但第三个项目网格显示在第一个列表视图上!!!)

我认为这与相对来源有关,但我找不到答案。 希望大家帮帮我。

【问题讨论】:

    标签: wpf listview mvvm datatemplate relativesource


    【解决方案1】:

    仅仅因为 ListBox 没有焦点,并不意味着项目被取消选中,我怀疑您在 ListViews 中将 SelectedItemSelectedIndex 绑定到相同的属性,这使得ListViewItem.IsSelected 在两个 ListView 之间同步

    我建议让您的网格的可见性条件基于 2 个属性而不是 1 个:ListViewItem.IsSelected 为真,并且如果 ListViewItem.IsKeyboardFocusWithin 设置为真。

    这是一个使用DataTrigger的示例

    <Style TargetType="{x:Type Grid}" x:Key="GridStyle">
        <Setter Property="Visibility" Value="Collapsed" />
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Value="True" Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}}" />
                    <Condition Value="True" Binding="{Binding Path=IsKeyboardFocusWithin, RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}}" />
                </MultiDataTrigger.Conditions>
                <Setter Property="Visibility" Value="Visible" />
            </MultiDataTrigger>
        </Style.Triggers>
    </Style>
    

    实际上回想起来,我认为IsKeyboardFocusWithin会将项目设置为选中,所以也许你只需要使用IsKeyboardFocusWithin而不是IsSelected

    【讨论】:

    • 回家后试试这个。认为你会再次成为我的英雄 Rachel n1! :D
    • oke 它可以工作,此外,现在如果我单击列表视图的一个项目,则另一个打开的列表视图项目(网格可见)现在关闭。有没有办法解决这个问题?
    • @Maximc 如果您想为所选项目保持可见的Grid,我会使用IsSelected 而不是IsKeyboardFocusWithin,并确保绑定SelectedItemSelectedIndex您的 ListViews 到两个不同的值。听起来它们绑定到相同的值,所以选择是同步的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-11
    • 2013-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多