【问题标题】:Add ContextMenu to usercontrols in WrapPanel将 ContextMenu 添加到 WrapPanel 中的用户控件
【发布时间】:2011-08-14 04:58:52
【问题描述】:

我在 WP7 应用程序中从 Silverlight 工具包中获得了 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 上?

【问题讨论】:

    标签: xaml windows-phone-7 binding contextmenu datacontext


    【解决方案1】:

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

    可以这么说,这里有一个 sn-p,它展示了如何在代码隐藏中绑定到一个属性(正是你需要做的):

    Binding b = new Binding();
    b.Source = ObjectToBindTo;
    b.Path = new PropertyPath("PropertyToBindTo");
    
    menu.SetBinding(DependencyPropertyToBind, b);
    

    话虽如此,您的情况还有一个问题。 ContextMenuWrapPanel 内 - 它与它绑定,而不是与正方形绑定。因此,您可能会更改使用 ContextMenu 的方式,使其位于方形实例而不是公共容器中。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多