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. Basically, I have two HubTiles on my MainPage, and each use the same ContextMenu click event. 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. What I have is as follows, but I do not know how to expose the properties of the HubTile where the ContextMenu click occured
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);
}
}
PlacementTargetis 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.