在 ContextMenu.MenuItem DataTemplate 中使用 ScrollViewer
我花了很多时间试图理解并实现一些看起来很简单的东西。
在我的 WP7 应用程序中,我有一个按钮,长按后会显示上下文菜单。由于此上下文菜单绑定到列表,因此项目数量可能会很大。到目前为止,我还无法在数据模板周围添加滚动查看器。但我已经测试过,如果数据模板不存在,它应该可以正常工作。
这是我的 XAML:
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu ItemsSource="{Binding}">
<toolkit:ContextMenu.ItemTemplate>
<DataTemplate >
<toolkit:MenuItem Header="{Binding Path=Name}" Click="MenuItem_Click"/>
</DataTemplate>
</toolkit:ContextMenu.ItemTemplate>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
我尝试在几乎所有位置添加 ScrollViewer(在标签之前:
、标签
,...) 但没有任何作用
我也尝试在我的标签中使用附加属性:
<toolkit:ContextMenu ItemsSource="{Binding}" ScrollViewer.VerticalScrollBarVisibility="Visible">
但它也不起作用。
但如果我不使用如下数据模板:
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu>
<ScrollViewer>
<stackPanel>
<toolkit:MenuItem Header="Item1"/>
<toolkit:MenuItem Header="Item2"/>
<toolkit:MenuItem Header="Item3"/>
<toolkit:MenuItem Header="Item4"/>
</stackPanel>
</ScrollViewer>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
它工作正常。
我错过了什么吗?
I've spent a lot of time trying to understand and to implement something that looks easy.
In my WP7 application, I have a button that displays a context menu after a long tap. As this context menu is bound to a list, the number of items can be huge. Until now, I haven't been able to add a scroll viewer around my data template. But I have tested that if the data template was not there, it should work fine.
Here is my XAML:
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu ItemsSource="{Binding}">
<toolkit:ContextMenu.ItemTemplate>
<DataTemplate >
<toolkit:MenuItem Header="{Binding Path=Name}" Click="MenuItem_Click"/>
</DataTemplate>
</toolkit:ContextMenu.ItemTemplate>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
I tried to add a ScrollViewer almost everywhere (before the tag: <toolkit:ContextMenuService.ContextMenu>
, before the tag <toolkit:ContextMenu ItemsSource="{Binding}">
,...) but nothing works
I also tried to use an attached property in my tag:
<toolkit:ContextMenu ItemsSource="{Binding}" ScrollViewer.VerticalScrollBarVisibility="Visible">
but it doesn't work either.
But if I don't use a data template like:
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu>
<ScrollViewer>
<stackPanel>
<toolkit:MenuItem Header="Item1"/>
<toolkit:MenuItem Header="Item2"/>
<toolkit:MenuItem Header="Item3"/>
<toolkit:MenuItem Header="Item4"/>
</stackPanel>
</ScrollViewer>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
it works fine.
Did I miss something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将 ScrollViewer 放入 Template 中,将 StackPanel 放入 ItemsPanelTemplate 中,如下所示:
You will want to put your ScrollViewer in the Template and a StackPanel in your ItemsPanelTemplate, so something like this: