如何访问正在呈现的

如何访问正在呈现的

本文介绍了如何访问正在呈现的 ListBox 中的项目而不是其源数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题的延续

我尝试使用链接答案中提供的解决方案,但是当我给它提供 ListBox 绑定到的图像时,它给了我错误的位置,因为项目模板正在加载源 URL 而不是实际图像,而且数据似乎是空的.

I tried using the solution provided in the linked answer, however when I give it the image which the ListBox is binding to, it gives me the wrong position, because the Item Template is loading in a source URL rather than the actual image, and it seems the data is null.

我的项目模板如下所示:

My Item Template looks like this:

<ListBox ItemsSource="{Binding Items}"
                     HorizontalContentAlignment="Left"
                     VerticalContentAlignment="Center"
                     ScrollViewer.VerticalScrollBarVisibility="Disabled"
                     ScrollViewer.HorizontalScrollBarVisibility="Visible"

                     Height="64" Name="listBoxSource"
                     VerticalAlignment="Bottom"
                     SelectionChanged="listBoxSource_SelectionChanged" Canvas.Left="32" Canvas.Top="365" Width="596"
             >

                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                        <Image x:Name="ListImage" Source="{Binding thumbnailURL}" Height="64" Width="64"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>

            </ListBox>

如何访问当前所选项目的ListImage"?

How do I get access to "ListImage" for the currently selected Item?

推荐答案

每个 ItemsControl 都有一个 ItemsContainerGenerator,它(出人意料地)负责为列表中的每个项目生成一个控件.它提供了一些有用的方法来查找给定项目的容器,反之亦然.你可以这样使用它:

Every ItemsControl has an ItemsContainerGenerator, which (surprisingly enough) is responsible for generating a control for each item in the list. It provides a few useful methods for finding the container of a given item, and vice versa. You can use it like this:

private void listBoxSource_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var listBox = (ListBox) sender;
    var containerGenerator = listBox.ItemContainerGenerator;
    var container = (UIElement)containerGenerator.ContainerFromItem(listBox.SelectedItem);
}

然后您可以使用变量 container 和其他帖子中的解决方案来查找坐标,

You can then use the variable container with the solution from your other post to find the coordinates,

Point position = container.GetRelativePosition(Application.Current.RootVisual);

顺便提一下,在您的 DataTemplate 中,您不需要 StackPanel,因为 ListBox 提供了它的 ItemsPanelTemplate.

And on a side note, in your DataTemplate you don't need the StackPanel, since the ListBox is providing that with its ItemsPanelTemplate.

<DataTemplate>
    <Image x:Name="ListImage" Source="{Binding thumbnailURL}" Height="64" Width="64"/>
</DataTemplate>

这篇关于如何访问正在呈现的 ListBox 中的项目而不是其源数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 00:29