简体   繁体   English

如何获取上下文菜单单击发生位置的父对象属性

[英]How to get the parent object properties of where a context menu click occured

I am trying to create secondary live tiles for my application based upon the click event of a contextmenu, and I need to expose some properties of the ContextMenu's parent object but I am unsure of how to do this. 我正在尝试根据contextmenu的click事件为我的应用程序创建辅助活动磁贴,并且需要公开ContextMenu父对象的某些属性,但是我不确定如何执行此操作。 Basically, I have two HubTiles on my MainPage, and each use the same ContextMenu click event. 基本上,我的MainPage上有两个HubTiles,每个都使用相同的ContextMenu click事件。 In the ContextMenu click event MenuItem_Tap , I call a method CreateLiveTile which should get properties of the HubTile where the contextmenu click occured for use in the secondary tile that will be pinned to the start screen. 在ContextMenu单击事件MenuItem_Tap ,我调用方法CreateLiveTile ,该方法应获取HubTile的属性,在该菜单中发生上下文菜单单击,以用于将固定在开始屏幕上的辅助磁贴中。 What I have is as follows, but I do not know how to expose the properties of the HubTile where the ContextMenu click occured 我所拥有的如下,但是我不知道如何公开发生ContextMenu单击的HubTile的属性。

MainPage.xaml MainPage.xaml

 <ListBox Grid.Row="0" x:Name="tileList" toolkit:TiltEffect.IsTiltEnabled="True">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <toolkit:WrapPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <toolkit:HubTile Title="{Binding Title}" Margin="3"
                                         Notification="{Binding Notification}"
                                         DisplayNotification="{Binding DisplayNotification}"
                                         Message="{Binding Message}"
                                         GroupTag="{Binding GroupTag}"
                                         Source="{Binding ImageUri}"
                                         Tap="hubTile_Tap">
                            <toolkit:ContextMenuService.ContextMenu>
                                <toolkit:ContextMenu x:Name="menu">
                                    <toolkit:MenuItem Header="pin to start" Tap="MenuItem_Tap"/>
                                </toolkit:ContextMenu>
                            </toolkit:ContextMenuService.ContextMenu>
                        </toolkit:HubTile>

                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

MainPage.xaml.cs MainPage.xaml.cs

public MainPage()
    {
        InitializeComponent();

        CreateHubTiles();
    }

private void CreateHubTiles()
    {
        List<TileItem> tileItems = new List<TileItem>() 
        {
            new TileItem() { ImageUri = "/Images/shareStatusImage.jpg", Title = "status", /*Notification = "last shared link uri",*/ Message = Settings.statusMessage.Value, GroupTag = "TileGroup" },
            new TileItem() { ImageUri = "/Images/shareLinkImage.jpg", Title = "link", /*Notification = "last shared status message",*/ Message = "last shared link uri", GroupTag = "TileGroup" }, 
        };

        this.tileList.ItemsSource = tileItems;
    }

private void MenuItem_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        //How to expose which HubTile was selected and its properties?
        //HubTile tap = sender as HubTile;  //not working, sent from ContextMenu not HubTile

        //(sender as MenuItem).
        //(sender as UIElement).
        //string parent = VisualTreeHelper.GetParent((sender as DependencyObject)).ToString();

        //attempt to pass the selected HubTile instance to CreateLiveTiles method
        CreateLiveTile(tap);
    }

    private void CreateLiveTile(HubTile hubtile)
    {
        string _title = hubtile.Title.ToString();

        //var Tile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("DefaultTitle=" + LiveTile.Title));
        var Tile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("DefaultTitle=" + _title));

        if (Tile == null)
        {
            try
            {
                var LiveTile = new StandardTileData
                {
                    Title = hubtile.Title,
                    BackgroundImage = ((System.Windows.Media.Imaging.BitmapImage)hubtile.Source).UriSource,
                    //Count = 1,
                    BackTitle = hubtile.Title,
                    //BackBackgroundImage = new Uri("", UriKind.Relative),
                    BackContent = hubtile.Message,
                };

                ShellTile.Create(new Uri("/MainPage.xaml?DefaultTitle=" + LiveTile.Title, UriKind.Relative), LiveTile);
            }
            catch (Exception)
            {
                MessageBox.Show("This tile could not be pinned", "Warning", MessageBoxButton.OK);
            }
        }
        else
        {
            MessageBox.Show("This tile has already been pinned", "Notice", MessageBoxButton.OK);
        }
    }

PlacementTarget is what you looking for, if sender is MenuItem then its parent will be ContextMenu and from that you can get the PlacementTarget where this contextMenu is placed. PlacementTarget是您要查找的内容,如果发件人是MenuItem,则其父项将是ContextMenu,从中您可以获取放置该contextMenu的PlacementTarget。

private void MenuItem_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
   HubTitle tap = (((sender as MenuItem).Parent as ContextMenu).PlacementTarget
                                                            as HubTitle);

   //attempt to pass the selected HubTile instance to CreateLiveTiles method
   CreateLiveTile(tap);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM