如何在WPF ListView中获取所选项容器的坐标

时间:2022-11-27 14:27:18

I want to display some WPF elements near to the selected item of a ListView. How can I obtain the coordinates (screen or relative) of the selected ListViewItem?

我想在ListView的选定项目附近显示一些WPF元素。如何获取所选ListViewItem的坐标(屏幕或相对)?

<ListView 
    x:Name="TechSchoolListView"
    ClipToBounds="False"
    Width="Auto" Height="Auto" 
    HorizontalContentAlignment="Stretch" 
    VerticalContentAlignment="Top" 
    ItemTemplate="{DynamicResource TechSchoolDataTemplate}" 
    ItemsSource="{Binding Path=TechSchoolResearchList, Mode=Default}" 
    SelectedIndex="1"
    SelectedValue="{Binding Path=SelectedTechSchool, Mode=Default}" 
    SelectionChanged="TechSchoolList_SelectionChanged" 
    ItemContainerStyle="{DynamicResource TechSchoolItemContainerStyle}" 
    ScrollViewer.CanContentScroll="False" 
    ScrollViewer.VerticalScrollBarVisibility="Disabled" >
    <ListView.Background>
        <SolidColorBrush Color="{DynamicResource PanelBackgroundColor}"/>
    </ListView.Background>
</ListView>

2 个解决方案

#1


2  

You should use ContainerFromElement to get the item's container, which is a visual and from there you can get the coordinates. You can't express this in XAML, however. You need to do it in code, on one of the ListView events, raised when the selected item is changed. Btw, keep in mind that the item can be its own container.

您应该使用ContainerFromElement来获取项目的容器,这是一个可视化的,从那里您可以获得坐标。但是,您无法在XAML中表达这一点。您需要在其中一个ListView事件的代码中执行此操作,并在更改所选项目时引发。顺便说一句,请记住,该项目可以是自己的容器。

You can't do this in XAML, as there's no attached property on the item that shows the item is selected. (though I haven't played with WPF in a while, so that might have changed)

您无法在XAML中执行此操作,因为项目上没有附加属性,表明该项目已被选中。 (虽然我有一段时间没有玩WPF,所以可能已经改变了)

#2


3  

Now I have found a solution by myself. I have searched for a simple property, but it made no sense, because all UI Elements in the WPF are relative.

现在我自己找到了解决方案。我搜索了一个简单的属性,但没有任何意义,因为WPF中的所有UI元素都是相对的。

This code seems to be working:

这段代码似乎有效:

        UIElement selectedContainer = (UIElement) TechSchoolListView.ItemContainerGenerator.ContainerFromIndex(TechSchoolListView.SelectedIndex);
        Point cursorPos = selectedContainer.TranslatePoint(new Point(selectedContainer.DesiredSize.Width, 0.0), Page);
        PanelCursor.Height = selectedContainer.DesiredSize.Height;
        PanelCursor.Margin = new Thickness(400, cursorPos.Y, 0.0, 0.0);

#1


2  

You should use ContainerFromElement to get the item's container, which is a visual and from there you can get the coordinates. You can't express this in XAML, however. You need to do it in code, on one of the ListView events, raised when the selected item is changed. Btw, keep in mind that the item can be its own container.

您应该使用ContainerFromElement来获取项目的容器,这是一个可视化的,从那里您可以获得坐标。但是,您无法在XAML中表达这一点。您需要在其中一个ListView事件的代码中执行此操作,并在更改所选项目时引发。顺便说一句,请记住,该项目可以是自己的容器。

You can't do this in XAML, as there's no attached property on the item that shows the item is selected. (though I haven't played with WPF in a while, so that might have changed)

您无法在XAML中执行此操作,因为项目上没有附加属性,表明该项目已被选中。 (虽然我有一段时间没有玩WPF,所以可能已经改变了)

#2


3  

Now I have found a solution by myself. I have searched for a simple property, but it made no sense, because all UI Elements in the WPF are relative.

现在我自己找到了解决方案。我搜索了一个简单的属性,但没有任何意义,因为WPF中的所有UI元素都是相对的。

This code seems to be working:

这段代码似乎有效:

        UIElement selectedContainer = (UIElement) TechSchoolListView.ItemContainerGenerator.ContainerFromIndex(TechSchoolListView.SelectedIndex);
        Point cursorPos = selectedContainer.TranslatePoint(new Point(selectedContainer.DesiredSize.Width, 0.0), Page);
        PanelCursor.Height = selectedContainer.DesiredSize.Height;
        PanelCursor.Margin = new Thickness(400, cursorPos.Y, 0.0, 0.0);