【问题标题】:How to get GridViewItem from its child element?如何从其子元素中获取 GridViewItem?
【发布时间】:2012-09-01 06:59:57
【问题描述】:

假设我在 XAML 中有这样的代码:

<GridView>
  <GridView.ItemTemplate>
<DataTemplate>
    <Button Content="{Binding test}" Click="ButtonClick" />
</DataTemplate>
</GridView.ItemTemplate>
</GridView>

那我怎样才能知道选择了哪个GridViewItem?因为,通常所做的是将 ItemClick 功能添加到 GridView 本身,但在这种情况下,我正在做一些定制的事情,需要从 Button 开始获取 SelectedItem。

我试过这样的代码:

void ButtonClick (object sender, RoutedEventArgs e)
{
   var g = (GridViewItem)((Button)sender).Parent;
}

但它不起作用(返回 null)。请帮忙!

谢谢!

【问题讨论】:

标签: c# xaml gridview windows-8


【解决方案1】:

当然!这是我在广告控件无法加载广告时使用的代码(例如机器离线时)。在这种情况下,我将其从网格视图中删除。为此,我必须找到广告的父 gridviewitem 并删除整个内容。我是这样做的:

private void AdControl_ErrorOccurred_1(object sender, Microsoft.Advertising.WinRT.UI.AdErrorEventArgs e)
{
    var _Item = sender as DependencyObject;
    while (!(_Item is GridViewItem))
        _Item = VisualTreeHelper.GetParent(_Item);
    HubGrid.Items.Remove(_Item);
}

【讨论】:

    猜你喜欢
    • 2019-02-19
    • 2019-12-17
    • 1970-01-01
    • 2021-01-05
    • 2013-08-24
    • 2019-05-15
    • 2013-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多