WP7 ContextMenu: answers to popular questions

转自:http://windowsphonegeek.com/tips/wp7-contextmenu-answers-to-popular-questions

by WindowsPhoneGeek

In the last few days we received lots of questions about how to use ContextMenu in WP7. In this mini tutorial we will give our answers to some of them.

NOTE: Before we begin let me first mention that ContextMenu is a part of the Silverlight for Windows Phone 7 toolkit. You can also take a look at our WP7 ContextMenu in depth | Part1: key concepts and API article for reference.

Question 1: Can I add Context Menu to DataTemplate ?

Question 2: I have ListBox with ContextMenu. How can I highlight the ListBox SelectedItem?

Question 3: I have ListBox with ContextMenu. How to get  a reference to the ListBox tapped item from the ContextMenu Click handler?

Answers: Yes you can add the ContextMenu in a DataTemplate. You can highlight the ListBox SelectedItem  and also get get  a reference to the ListBox tapped item from the ContextMenu Click handler in this way:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
< ListBox x:Name = "listBox" >
     < ListBox.ItemTemplate >
         < DataTemplate >
             < StackPanel x:Name = "sp" >
                 < toolkit:ContextMenuService.ContextMenu >
                     < toolkit:ContextMenu >
                         < toolkit:MenuItem Header = "Add Color" Click = "MenuItem_Click" />
                         < toolkit:MenuItem Header = "Remove Color" Click = "MenuItem_Click" />
                     </ toolkit:ContextMenu >
                 </ toolkit:ContextMenuService.ContextMenu >
                 < Image Source = "{Binding ImageUri}" Stretch = "None" />
                 < TextBlock Text = "{Binding Text}" />
             </ StackPanel >
         </ DataTemplate >
     </ ListBox.ItemTemplate >
</ ListBox >
?
1
2
3
4
5
6
7
8
9
10
11
12
public MainPage()
{
     InitializeComponent();
 
     ObservableCollection<SampleData> dataSource = new ObservableCollection<SampleData>();
 
     dataSource.Add( new SampleData() { ImageUri = "Images/appbar.close.rest.png" , Text = "Item1"  });
     dataSource.Add( new SampleData() { ImageUri = "Images/appbar.delete.rest.png" , Text = "Item2" });
     dataSource.Add( new SampleData() { ImageUri = "Images/appbar.download.rest.png" , Text = "Item3" });
 
     this .listBox.ItemsSource = dataSource;
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
     string header = (sender as MenuItem).Header.ToString();
 
     ListBoxItem selectedListBoxItem = this.listBox.ItemContainerGenerator.ContainerFromItem((sender as MenuItem).DataContext) as ListBoxItem;
     if (selectedListBoxItem == null)
     {
         return;
     }
 
     if (header == "Add Color")
     {
         selectedListBoxItem.Background = new SolidColorBrush(Colors.Red);
     }
     else
     {
         selectedListBoxItem.Background = new SolidColorBrush(Colors.Black);
     }
 
     //To highlight the tapped item just use something like selectedListBoxItem.Background = new SolidColorBrush(Colors.Green);
}

    

Question 4How to populate the WP7 ContextMenu programmatically?

Answer: You can populate the ContextMenu programmatically either using MenuItems or through its ItemSource property.

Example1. Populate the ContextMenu with MenuItems:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public MainPage()
{
     InitializeComponent();
     this .AddContextMenuWithMenuItems();
}
 
private void AddContextMenuWithMenuItems()
{
     ContextMenu contextMenu = new ContextMenu();
     MenuItem menuItem1 = new MenuItem() { Header = "Add" , Tag = "Add" };
     MenuItem menuItem2 = new MenuItem() { Header = "Remove" , Tag = "Remove" };
     MenuItem menuItem3 = new MenuItem() { Header = "Cancel" , Tag = "Cancel" };
     contextMenu.Items.Add(menuItem1);
     contextMenu.Items.Add(menuItem2);
     contextMenu.Items.Add(menuItem3);
     ContextMenuService.SetContextMenu( this .ContentPanel,contextMenu);
}

NOTE: ControlPanel is the name of the UIElement to which you want to associate the menu.

Example2. Populate the ContextMenu through its ItemsSource property:

?
1
2
3
4
5
6
7
8
9
10
11
12
public MainPage()
 
     InitializeComponent();
     this .AddContextMenuWithBinding();
}
 
private void AddContextMenuWithBinding()
{
     ContextMenu contextMenu = new ContextMenu();
     contextMenu.ItemsSource = new List< string > { "Add" , "Remove" , "Cancel" };
     ContextMenuService.SetContextMenu( this .ContentPanel, contextMenu);
}

Question 5: How can I open the ContextMenu on Tap instead of Tap and Hold?

Answer: By default the context menu opens on Tab and Hold. Lets say that we have an Image and we want to open the menu only when users Tap the image (not Tab + Hold but only Tap). The code for accomplishing this is as follows:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
< Grid >
     < toolkit:ContextMenuService.ContextMenu >
         < toolkit:ContextMenu x:Name = "menu1" >
             < toolkit:MenuItem Header = "item1" />
             < toolkit:MenuItem Header = "item2"  />
             < toolkit:MenuItem Header = "item3"  />
         </ toolkit:ContextMenu >
     </ toolkit:ContextMenuService.ContextMenu >
     < Image Source = "logo.png" Height = "80" Width = "80" >
         < toolkit:GestureService.GestureListener >
             < toolkit:GestureListener Tap = "GestureListener_Tap" />
         </ toolkit:GestureService.GestureListener >
     </ Image >
     < TextBlock Text = "Tap the image" />
</ Grid >
?
1
2
3
4
5
6
7
private void GestureListener_Tap( object sender, GestureEventArgs e)
{
     if ( this .menu1.Parent == null )
     {
         this .menu1.IsOpen = true ;
     }
}

I hope that this mini tutorial was helpful. Here is the full source code.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值