获取启动弹出窗口的按钮

时间:2016-04-11 12:38:26

标签: c# xaml win-universal-app

我有一个GridView,我有一个弹出按钮。我在MenuFlyoutItems上有两个方法,我需要知道调用弹出按钮的按钮属性。有办法吗?
我的代码:

- (void)didGetTranslation:(CGPoint *)translation {
    NSLog(@"cgpointed");
}

1 个答案:

答案 0 :(得分:1)

ContainerFromItem返回GridViewItem。您可以使用VisualTreeHelper获取按钮元素

   private void Remove_Click(object sender, RoutedEventArgs e)
    {
        MenuFlyoutItem mfi = (MenuFlyoutItem)sender;
        var datacontext = mfi.DataContext;
        GridViewItem item = grd.ContainerFromItem(datacontext) as GridViewItem ;
        Button button = FindElementInVisualTree<Button>(item);
    }
 private T FindElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
        {
            var count = VisualTreeHelper.GetChildrenCount(parentElement);
            if (count == 0) return null;

            for (int i = 0; i < count; i++)
            {
                var child = VisualTreeHelper.GetChild(parentElement, i);
                if (child != null && child is T)
                    return (T)child;
                else
                {
                    var result = FindElementInVisualTree<T>(child);
                    if (result != null)
                        return result;
                }
            }
            return null;
        }