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

时间:2012-08-15 06:06:14

标签: c# windows-phone-7 xaml contextmenu

我正在尝试根据上下文菜单的click事件为我的应用程序创建辅助动态磁贴,我需要公开ContextMenu的父对象的一些属性,但我不确定如何执行此操作。基本上,我的MainPage上有两个HubTiles,每个都使用相同的ContextMenu点击事件。在ContextMenu单击事件MenuItem_Tap中,我调用方法CreateLiveTile,该方法应获取HubTile的属性,其中发生上下文菜单单击以在将固定到开始屏幕的辅助磁贴中使用。我的内容如下,但我不知道如何公开出现ContextMenu点击的HubTile的属性

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中

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);
        }
    }

1 个答案:

答案 0 :(得分:0)

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);
}