将 ContextMenu 添加到 WrapPanel 中的用户控件

发布于 2024-11-03 20:51:07 字数 1392 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

穿越时光隧道 2024-11-10 20:51:07

显然,ContextMenu 有一个不同的DataContext。每个方块中的绑定与此处的 WrapPanel 无关,您必须在代码隐藏中手动设置 ContextMenu 的绑定对象。

可以这么说,这是一个片段,展示了如何绑定到代码隐藏中的属性(正是您需要做的):

Binding b = new Binding();
b.Source = ObjectToBindTo;
b.Path = new PropertyPath("PropertyToBindTo");

menu.SetBinding(DependencyPropertyToBind, b);

话虽这么说,您的情况还有一个问题。 ContextMenu 位于 WrapPanel 内部 - 它与 WrapPanel 相关联,而不是与方块相关联。因此,您可以更改在方形实例而不是公共容器中使用 ContextMenu 的方式。

Apparently the ContextMenu has a different DataContext. Binding in each square has nothing to do with the WrapPanel here and you will have to manually set the binding object for the ContextMenu 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):

Binding b = new Binding();
b.Source = ObjectToBindTo;
b.Path = new PropertyPath("PropertyToBindTo");

menu.SetBinding(DependencyPropertyToBind, b);

That being said, there is one more problem in your case. The ContextMenu is inside a WrapPanel - it is tied to it and not to the square. Therefore, you might change the way you are using the ContextMenu to be inside the square instance rather than the common container.

故事还在继续 2024-11-10 20:51:07

感谢丹尼斯的回答,这就是我的最终结果。我将列表框与包装面板结合使用,以便能够滚动方块列表。

<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;

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.

<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>

and in my codebehind;

MonitoredCombinedFlightsList.ItemsSource = Cache.MonitoredCombinedFlights;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文