2010-11-17 63 views
1

綁定文本菜單,我有:中的DataTemplate

<ListBox> 
      <ListBox.Resources> 
       <DataTemplate DataType="{x:Type ViewModels:StyleViewModel}"> 
        <DockPanel>        
         <Button Content="{Binding Name}" Tag="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}"> 
          <Button.ContextMenu> 
           <ContextMenu> 
            <MenuItem Header="Delete" Command="{Binding PlacementTarget.Tag.DataContext.RemoveMember1FavoriteStyleCommand}" CommandParameter="{Binding}" /> 
           </ContextMenu> 
          </Button.ContextMenu> 
         </Button>        
        </DockPanel> 
       </DataTemplate> 
      </ListBox.Resources> 
     </ListBox> 

我試圖做到的,是的命令,在右鍵菜單中一個ICommand是在視圖模型定義是在DataContext的菜單項綁定列表框和命令參數應該是StyleViewModel,但是我試過的東西沒有工作。任何人都可以將我指向正確的方向嗎?

回答

4

找到了!

<ListBox ItemsSource="{Binding ActiveCustomer.Member1FavoriteStyles}" ItemsPanel="{StaticResource ListBoxStyleItemsPanelAsVerticalStackPanel}" ItemContainerStyle="{StaticResource ListBoxItemContainerStyle}" Background="Transparent" BorderThickness="0"> 
      <ListBox.Resources> 
       <DataTemplate DataType="{x:Type ViewModels:StyleViewModel}"> 
        <DockPanel>        
         <Button Content="{Binding Name}" Tag="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}"> 
          <Button.ContextMenu> 
           <ContextMenu> 
            <MenuItem Header="Remove" Command="{Binding PlacementTarget.Tag.RemoveMember1FavoriteStyleCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}" CommandParameter="{Binding}" /> 
           </ContextMenu> 
          </Button.ContextMenu> 
         </Button>        
        </DockPanel> 
       </DataTemplate> 
      </ListBox.Resources> 
     </ListBox> 
1

現在差​​不多的工作,只是現在CommandParameter = 「{結合}」 不返回StyleViewModel:

<ListBox ItemsSource="{Binding ActiveCustomer.Member1FavoriteStyles}" ItemsPanel="{StaticResource ListBoxStyleItemsPanelAsVerticalStackPanel}" ItemContainerStyle="{StaticResource ListBoxItemContainerStyle}" Background="Transparent" BorderThickness="0"> 
      <ListBox.Resources> 
       <DataTemplate DataType="{x:Type ViewModels:StyleViewModel}"> 
        <DockPanel> 
         <Button Content="{Binding Name}" Tag="{Binding DataContext,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}"> 
          <Button.ContextMenu> 
           <ContextMenu DataContext="{Binding RelativeSource={RelativeSource Mode=Self}, Path=PlacementTarget.Tag}"> 
            <MenuItem Header="{Binding ActiveCustomer.Member1FirstName}" Command="{Binding RemoveMember1FavoriteStyleCommand}" CommandParameter="{Binding}" /> 
           </ContextMenu> 
          </Button.ContextMenu> 
         </Button>        
        </DockPanel> 
       </DataTemplate> 
      </ListBox.Resources> 
     </ListBox> 

如果是可以做到的,我不知道...