将 ContextMenu 添加到 WrapPanel 中的用户控件
我在 WP7 应用程序中从 Silverlight Toolkit 获得了一个 WrapPanel。在我的代码隐藏中,我将自己的用户控件添加到此包装面板中。每个用户控件都是一个显示航班信息的正方形。
<ScrollViewer x:Name="MyScrollViewer" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Height="307" VerticalAlignment="Bottom">
<toolkit:WrapPanel x:Name="MonitoredWrapPanel" Margin="10,0,0,0" MinWidth="200" Width="{Binding ElementName=MyScrollViewer, Path=ViewportWidth}">
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu x:Name="FlightContextMenuInGrid">
<toolkit:MenuItem Header="Stop monitoring flight" Click="MenuItem_Click" Tag="{Binding Path=FlightId}" />
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</toolkit:WrapPanel>
</ScrollViewer>
和代码隐藏;
foreach (var flight in Cache.MonitoredCombinedFlights)
{
FlightSquare square = new FlightSquare();
square.DataContext = flight;
square.Margin = new Thickness(10, 5, 10, 5);
this.MonitoredWrapPanel.Children.Add(square);
}
我的问题是 MenuItem(对于 ContextMenu)没有绑定到我的用户控件的 DataContext 的 FlightId 属性,而是仅绑定到自身。
我怎样才能让 MenuItem 了解它位于哪个 FlightSquare 上?
I've got a WrapPanel from the Silverlight Toolkit in a WP7 application. In my codebehind I add my own usercontrols to this wrappanel. Each usercontrol is a square that displays information about a flight.
<ScrollViewer x:Name="MyScrollViewer" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Height="307" VerticalAlignment="Bottom">
<toolkit:WrapPanel x:Name="MonitoredWrapPanel" Margin="10,0,0,0" MinWidth="200" Width="{Binding ElementName=MyScrollViewer, Path=ViewportWidth}">
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu x:Name="FlightContextMenuInGrid">
<toolkit:MenuItem Header="Stop monitoring flight" Click="MenuItem_Click" Tag="{Binding Path=FlightId}" />
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</toolkit:WrapPanel>
</ScrollViewer>
And the codebehind;
foreach (var flight in Cache.MonitoredCombinedFlights)
{
FlightSquare square = new FlightSquare();
square.DataContext = flight;
square.Margin = new Thickness(10, 5, 10, 5);
this.MonitoredWrapPanel.Children.Add(square);
}
My problem is that the MenuItem (for the ContextMenu) doesn't bind to the FlightId property of the DataContext for my User Control, instead it just binds to itself.
How can I get the MenuItem to understand which FlightSquare it is on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
显然,
ContextMenu
有一个不同的DataContext
。每个方块中的绑定与此处的WrapPanel
无关,您必须在代码隐藏中手动设置ContextMenu
的绑定对象。可以这么说,这是一个片段,展示了如何绑定到代码隐藏中的属性(正是您需要做的):
话虽这么说,您的情况还有一个问题。
ContextMenu
位于WrapPanel
内部 - 它与 WrapPanel 相关联,而不是与方块相关联。因此,您可以更改在方形实例而不是公共容器中使用 ContextMenu 的方式。Apparently the
ContextMenu
has a differentDataContext
. Binding in each square has nothing to do with theWrapPanel
here and you will have to manually set the binding object for theContextMenu
in the code-behind.So to say, here is a snippet that shows how you can bind to a property in code-behind (exactly what you need to do):
That being said, there is one more problem in your case. The
ContextMenu
is inside aWrapPanel
- it is tied to it and not to the square. Therefore, you might change the way you are using theContextMenu
to be inside the square instance rather than the common container.感谢丹尼斯的回答,这就是我的最终结果。我将列表框与包装面板结合使用,以便能够滚动方块列表。
在我的代码隐藏中;
Thanks for the answer Dennis, here is what I ended up with. I'm using a listbox in combination with the wrappanel to be able scroll the list of squares.
and in my codebehind;