Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 8 Metro:如何单击GridViewItem?_Gridview_Windows 8_Microsoft Metro - Fatal编程技术网

Windows 8 Metro:如何单击GridViewItem?

Windows 8 Metro:如何单击GridViewItem?,gridview,windows-8,microsoft-metro,Gridview,Windows 8,Microsoft Metro,我正在构建一个带有GridView的Windows8Metro应用程序C、XAML,我想在用户单击的GridViewItem附近显示一个弹出窗口。我计划通过click事件参数获取GridViewItem元素,并使用其坐标来确定放置弹出窗口的位置 但是,我还没有找到一种方法来获取对实际单击的GridViewItem UIElement的引用!我在下面尝试的事件似乎只通过其EventArgs公开DataContext,而不是实际的UIElement: object sender // is the

我正在构建一个带有GridView的Windows8Metro应用程序C、XAML,我想在用户单击的GridViewItem附近显示一个弹出窗口。我计划通过click事件参数获取GridViewItem元素,并使用其坐标来确定放置弹出窗口的位置

但是,我还没有找到一种方法来获取对实际单击的GridViewItem UIElement的引用!我在下面尝试的事件似乎只通过其EventArgs公开DataContext,而不是实际的UIElement:

object sender // is the GridView
ItemClickEventArgs.ClickedItem // is the DataContext of the GridViewItem
ItemClickEventArgs.OriginalSource // is the GridView
SelectionChangedEventArgs.OriginalSource // is null
SelectionChangedEventArgs.AddedItems.First() // is the DataContext of the GridViewItem
如果有必要,my GridView.ItemSource是一个CollectionViewSource,其源绑定到一个viewmodels集合


是否有可能通过我忽略的某个事件获取单击的GridViewItem?如果没有,我应该从什么角度来解决这个问题?我至少可以通过PointerPressed事件获取相对于GridView的点击坐标,并查看如何以这种方式定位该项,但我真的希望我不必走这条路。

VisualTreeHelper可能会帮助您将其放置到GridView/GridView项中,并找到它的子/父元素,如果您想找到UIElement。要确定UIElement是什么,应使用VisualTreeHelper解决容器内的单击

有关VisualTreeHelper的更多信息,请参见此处


如果您不想使用VisualTreeHelper,我将创建一个附加属性,所有我的GridViewItem单击将作为dependencyObject传递给GridView的附加属性,该属性是自定义的,您可以从中获得更多信息。

VisualTreeHelper可能会帮助您将其放置到GridView/GridViewItem中,并找到它的子级/父级,如果您想找到元素。要确定UIElement是什么,应使用VisualTreeHelper解决容器内的单击

有关VisualTreeHelper的更多信息,请参见此处


如果您不想使用VisualTreeHelper,我将创建一个附加属性,所有我的GridViewItem单击将作为dependencyObject传递给GridView的附加属性,该属性是自定义的,您可以从中获得更多信息。

我也在一个应用程序中实现了此行为-使用Callisto库中的弹出式按钮与单击的GridViewItem结合使用

简单地调用ContainerFromItem以获取相关的GridViewItem控件要容易得多:

private void itemGridView_ItemClick(object sender, ItemClickEventArgs e)
{
    var clickedItem = itemGridView.ItemContainerGenerator.ContainerFromItem(e.ClickedItem);

    // Open the flyout now
    Flyout flyOut = new Flyout();
    if (clickedItem is GridViewItem)
        flyOut.PlacementTarget = clickedItem as UIElement;
    else
        flyOut.PlacementTarget = sender as UIElement;

    flyOut.Placement = PlacementMode.Left;
    SolidColorBrush br = new SolidColorBrush(Colors.Blue);
    flyOut.Background = br;
    TextBlock tb = new TextBlock();
    tb.Text = "I'm in your flyout messing with your text";

    flyOut.Content = tb;
    flyOut.IsOpen = true;
}

感谢您为我指明了正确的方向。

我也在一个应用程序中实现了此行为-使用Callisto库中的弹出式按钮以及单击的GridViewItem

简单地调用ContainerFromItem以获取相关的GridViewItem控件要容易得多:

private void itemGridView_ItemClick(object sender, ItemClickEventArgs e)
{
    var clickedItem = itemGridView.ItemContainerGenerator.ContainerFromItem(e.ClickedItem);

    // Open the flyout now
    Flyout flyOut = new Flyout();
    if (clickedItem is GridViewItem)
        flyOut.PlacementTarget = clickedItem as UIElement;
    else
        flyOut.PlacementTarget = sender as UIElement;

    flyOut.Placement = PlacementMode.Left;
    SolidColorBrush br = new SolidColorBrush(Colors.Blue);
    flyOut.Background = br;
    TextBlock tb = new TextBlock();
    tb.Text = "I'm in your flyout messing with your text";

    flyOut.Content = tb;
    flyOut.IsOpen = true;
}

谢谢你给我指出了正确的方向。

谢谢,这就成功了。VisualTreeHelper.FindElementsInHostCoordinates是一种方法,它从PointerPressed事件返回给定点上的UIElements集合,然后可以向下过滤到GridViewItem。谢谢,这破解了它。VisualTreeHelper.FindElementsInHostCoordinates是一种方法,它从PointerPressed事件返回给定点上的UIElements集合,然后可以向下过滤到GridViewItem。