Xaml 将ContextMenu添加到WrapPanel中的usercontrols
我在WP7应用程序中获得了Silverlight工具包的包装。在我的codebehind中,我将自己的UserControl添加到这个包装中。每个用户控件都是一个显示航班信息的正方形Xaml 将ContextMenu添加到WrapPanel中的usercontrols,xaml,windows-phone-7,binding,contextmenu,datacontext,Xaml,Windows Phone 7,Binding,Contextmenu,Datacontext,我在WP7应用程序中获得了Silverlight工具包的包装。在我的codebehind中,我将自己的UserControl添加到这个包装中。每个用户控件都是一个显示航班信息的正方形 <ScrollViewer x:Name="MyScrollViewer" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Height="307" VerticalAlignment="Botto
<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>
我的问题是MenuItem(用于ContextMenu)没有绑定到我的用户控件的DataContext的FlightId属性,而只是绑定到它自己
如何让菜单项了解它在哪个FlightSquare上?显然,
ContextMenu
具有不同的DataContext
。每个方块中的绑定与此处的WrapPanel
无关,您必须手动为代码隐藏中的ContextMenu
设置绑定对象
也就是说,下面是一个片段,展示了如何在代码隐藏中绑定到属性(这正是您需要做的):
话虽如此,你的情况还有一个问题。
ContextMenu
位于一个WrapPanel
内-它与之绑定,而不是与正方形绑定。因此,您可能会将使用上下文菜单的方式更改为在方形实例中而不是在公共容器中。谢谢您的回答Dennis,以下是我的结论。我正在使用一个列表框和wrappanel来滚动方块列表
<ListBox Height="311" HorizontalAlignment="Left" Margin="0,323,0,0" Name="MonitoredCombinedFlightsList" VerticalAlignment="Top" Width="450">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel x:Name="MonitoredWrapPanel" Margin="10,0,0,0" MinWidth="200">
</toolkit:WrapPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Views:FlightSquare Margin="10,5,10,5">
<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>
</Views:FlightSquare>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Binding b = new Binding();
b.Source = ObjectToBindTo;
b.Path = new PropertyPath("PropertyToBindTo");
menu.SetBinding(DependencyPropertyToBind, b);
<ListBox Height="311" HorizontalAlignment="Left" Margin="0,323,0,0" Name="MonitoredCombinedFlightsList" VerticalAlignment="Top" Width="450">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel x:Name="MonitoredWrapPanel" Margin="10,0,0,0" MinWidth="200">
</toolkit:WrapPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Views:FlightSquare Margin="10,5,10,5">
<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>
</Views:FlightSquare>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
MonitoredCombinedFlightsList.ItemsSource = Cache.MonitoredCombinedFlights;