WPF ListViewItem 失去焦点事件 - 如何获取该事件?

发布于 2024-07-11 13:28:35 字数 1485 浏览 8 评论 0原文

我有一个列表视图,您可以在其中选择行/项目。 它链接到在行上显示图像的数据触发器。 仅当选择该行时才应显示图像。

这部分工作正常,但是当您将焦点移至其他内容(例如文本框)或显示消息框时,列表视图项目将失去焦点,即不再显示行上的突出显示。 问题是我的形象仍然存在。 当列表视图失去焦点时,它应该隐藏/折叠...如果您在列表视图上选择不同的项目/行,它可以正常工作。

有人可以帮忙吗?

<Style x:Key="deleteImageStyle" TargetType="{x:Type Image}">
    <Setter Property="Source" Value="Resources/iconDelete.png" />
    <Setter Property="Margin" Value="0,2,5,0" />
    <Setter Property="Height" Value="16" />
    <Setter Property="Width" Value="16" />
    <Setter Property="HorizontalAlignment" Value="Right" />
    <Setter Property="VerticalAlignment" Value="Top" />
    <Setter Property="Cursor" Value="Hand" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="True">
            <Setter Property="Visibility" Value="Visible"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="False">
            <Setter Property="Visibility" Value="Hidden"/>
        </DataTrigger>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Visibility" Value="Hidden" />
        </Trigger>
    </Style.Triggers>
</Style>

问候

特拉维斯PUK

I have a listview that you select a row/item on. This is linked to a datatrigger that displays an image on the row. The image should only be displayed when the row is selected.

This part works fine, however when you move the focus to something else, such as a textbox, or a messagebox is displayed, the listviewitem loses focus ie the highlight on the row is no longer displayed. The problem is that my image still remains. It should be hidden/collapsed when the listview loses focus... It works fine if you select a different item/row on the listview.

Can anyone help on this please?

<Style x:Key="deleteImageStyle" TargetType="{x:Type Image}">
    <Setter Property="Source" Value="Resources/iconDelete.png" />
    <Setter Property="Margin" Value="0,2,5,0" />
    <Setter Property="Height" Value="16" />
    <Setter Property="Width" Value="16" />
    <Setter Property="HorizontalAlignment" Value="Right" />
    <Setter Property="VerticalAlignment" Value="Top" />
    <Setter Property="Cursor" Value="Hand" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="True">
            <Setter Property="Visibility" Value="Visible"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="False">
            <Setter Property="Visibility" Value="Hidden"/>
        </DataTrigger>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Visibility" Value="Hidden" />
        </Trigger>
    </Style.Triggers>
</Style>

Regards

TravisPUK

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隔岸观火 2024-07-18 13:28:36

我认为您混淆了 IsSelected 和 IsFocused。

尝试将触发器绑定到 IsFocused IsSelected 以获得您想要的结果。

如果我理解正确的话,您只希望当 IsSelected 和 IsFocused 都为 true 时图像可见,否则隐藏。

执行此操作的一种方法是将默认可见性设置为可见,然后添加两个将可见性设置为隐藏的触发器:一个触发器用于 IsSelected = False,另一个触发器用于 IsFocused = False。

或者相反,将默认可见性设置为隐藏,并使用 IsSelected = True 和 IsFocused = True 的 MultiTrigger 将其可见性设置为可见

I think you're confusing IsSelected and IsFocused.

Experiment with binding your triggers to IsFocused instead of IsSelected to get your desired result.

If i understand correctly, you only want the image to be visible if both IsSelected and IsFocused are true, otherwise hidden.

One way to do this is to set default Visibility to Visible, and then add two triggers that set Visibility to Hidden: one trigger for IsSelected = False, and another trigger for IsFocused = False.

Or the opposite, set default Visibility to Hidden, and use a MultiTrigger with IsSelected = True and IsFocused = True to set it Visibility to Visible

转身泪倾城 2024-07-18 13:28:36

@Bubblewrap,

感谢您提供的信息,这很好地解决了这个问题。 如下所示,我必须在这两种情况下添加,因为默认值似乎没有生效...但是我还没有尝试过 MultiTrigger 方法,稍后会尝试。

这就是我最终得到的结果。

<Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="True">
                <Setter Property="Visibility" Value="Visible"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsFocused}" Value="True">
                <Setter Property="Visibility" Value="Visible"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="False">
                <Setter Property="Visibility" Value="Hidden"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsFocused}" Value="False">
                <Setter Property="Visibility" Value="Hidden"/>
            </DataTrigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Visibility" Value="Hidden" />
            </Trigger>
        </Style.Triggers>

感谢您的帮助,这将使我暂时解决我的问题。 我认为我的 IsEnabled 触发器现在可能是多余的。

谢谢

特拉维斯PUK

@Bubblewrap,

Thanks for the information, this pretty well got around the issue. As per below I had to add in both scenarios as the default doesn't seem to take effect... however I haven't tried the MultiTrigger method yet, will do later.

This is what I ended up with in the end.

<Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="True">
                <Setter Property="Visibility" Value="Visible"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsFocused}" Value="True">
                <Setter Property="Visibility" Value="Visible"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="False">
                <Setter Property="Visibility" Value="Hidden"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsFocused}" Value="False">
                <Setter Property="Visibility" Value="Hidden"/>
            </DataTrigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Visibility" Value="Hidden" />
            </Trigger>
        </Style.Triggers>

Thanks for your help, this will get me past my issue for now. I think that my IsEnabled trigger is probably redundant now though.

Thanks

TravisPUK

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文