WP7:将 ContextMenu 的数据模板内的 MenuItem 绑定到 List
我对 Xaml 开发(WPF、Sliverlight、WP7)相当陌生。
这是我的问题:
我有一个自定义按钮,当我单击它(长按或按住)时,我想打开与列表相关的特定上下文菜单。这意味着,由于我们单击的按钮,menuItem 标题具有不同的文本。
这是示例代码: 我的列表
public class ListTest
{
private List<string> testList;
public List<string> TestList
{
get { return testList; }
set { testList = value; }
}
public ListTest()
{
testList = new List<string>();
testList.Add("1st Header");
testList.Add("2nd Header");
testList.Add("3rd Header");
testList.Add("4th Header");
}
}
我的 Mainpage WP7
public MainPage()
{
InitializeComponent();
ListTest tst = new ListTest();
this.DataContext = tst.TestList;
}
我的 XAML 代码
<my:OwnButton IconSource="Image/beer.png" DrinkText="Beer" x:Name="btnBeer1">
</toolkit:GestureService.GestureListener>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu ItemsSource="{Binding}">
<toolkit:ContextMenu.ItemTemplate>
<DataTemplate >
<toolkit:MenuItem Header="{Binding}" />
</DataTemplate>
</toolkit:ContextMenu.ItemTemplate>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</my:OwnButton>
实际上,我认为我的上下文菜单菜单项将由列表自动填充,因为此列表被设置为 MainPage 的 DataContext。当我运行我的应用程序时,我的上下文菜单是空的。帮助?
I'm quite new in Xaml developpement (WPF,Sliverlight,WP7).
Here is my problem:
I've got a customized button, when I click on it (long click or hold), I'd like to open a specific contextMenu related to a List. It means, the menuItem header have a different text due to the button we clicked.
Here is a sample code:
My list
public class ListTest
{
private List<string> testList;
public List<string> TestList
{
get { return testList; }
set { testList = value; }
}
public ListTest()
{
testList = new List<string>();
testList.Add("1st Header");
testList.Add("2nd Header");
testList.Add("3rd Header");
testList.Add("4th Header");
}
}
my Mainpage WP7
public MainPage()
{
InitializeComponent();
ListTest tst = new ListTest();
this.DataContext = tst.TestList;
}
My XAML code
<my:OwnButton IconSource="Image/beer.png" DrinkText="Beer" x:Name="btnBeer1">
</toolkit:GestureService.GestureListener>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu ItemsSource="{Binding}">
<toolkit:ContextMenu.ItemTemplate>
<DataTemplate >
<toolkit:MenuItem Header="{Binding}" />
</DataTemplate>
</toolkit:ContextMenu.ItemTemplate>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</my:OwnButton>
Acutally, I tought my contextmenu menuitem will fill automaticily by the List as this is list is set as DataContext of the MainPage. When I run my application, my contextmenu is empty. Help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将数据模板更改为:
Try changing the data template to this: