2014-07-10 38 views
1

我有嵌套的DataGrid。嵌套網格顯示爲選定DataGrid的RowDetails。這是我使用DataGrid綁定的類結構如何在嵌套的DataGrid中綁定ContextMenu項目?

public class CustomTable : INotifyPropertyChanged 
    { 
      public List<DataTable> Main { get; set; } 
      private List<string> _menu; 
      public List<string> Menu 
      { 
       get 
       { 
        return _menu; 
       } 
       set 
       { 
        _menu = value 
        OnPropertyChanged("Menu"); 
       } 
      } 

      private CustomTable _child; 
      public CustomTable Child 
      { 
       get 
       { 
        return _child; 
       } 
       set 
       { 
        _child = value; 
        OnPropertyChanged("Child"); 
       } 
      } 
      public DataRowView _selectedItem; 
      public DataRowView SelectedItem 
      { 
       get 
       { 
        return _selectedItem; 
       } 
       set 
       { 
        _selectedItem = value; 
        Child = new CustomTable(); 
        OnPropertyChanged("SelectedItem"); 
       } 
      } 


     public CustomTable() 
     { 
      Main = new List<DataTable>(); 
      Main.Add(someRandomTable()); 

     } 

     private DataTable someRandomTable() 
     { 
      DataTable table = new DataTable(); 
      table.Columns.Add("Dosage", typeof(int)); 
      table.Columns.Add("Drug", typeof(string)); 
      table.Columns.Add("Patient", typeof(string)); 
      table.Columns.Add("Date", typeof(DateTime)); 
      table.Rows.Add(25, "Indocin", "David", DateTime.Now); 
      table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now); 
      table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now); 
      table.Rows.Add(21, "Combivent", "Janet", DateTime.Now); 
      table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now); 
      return table; 
     } 



     public event PropertyChangedEventHandler PropertyChanged; 

     protected void OnPropertyChanged(string caller) 
     { 

      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(caller)); 
      } 
     } 

} 

這是我的XAML來綁定datagrid和數據表。我想要將菜單列表綁定到DataGrid。由於

<ScrollViewer> 
<ScrollViewer.Resources> 
    <DataTemplate DataType="{x:Type l:CustomTable}"> 
     <StackPanel> 
      <ItemsControl ItemsSource="{Binding Path=Main}"> 
       <ItemsControl.ItemTemplate> 
        <DataTemplate> 
         <DataGrid Name="dg" SelectedItem="{Binding DataContext.SelectedItem, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor,AncestorType=ItemsControl}}" 
           CanUserAddRows="False" ItemsSource="{Binding}" AutoGenerateColumns="True" > 
          <DataGrid.RowDetailsTemplate> 
           <DataTemplate> 
            <ContentControl Margin="10" 
                Content="{Binding DataContext.Child, RelativeSource={RelativeSource FindAncestor,AncestorType=ItemsControl,AncestorLevel=2}}"/> 
           </DataTemplate> 
          </DataGrid.RowDetailsTemplate> 
         </DataGrid> 
        </DataTemplate> 
       </ItemsControl.ItemTemplate> 
      </ItemsControl> 
     </StackPanel> 
    </DataTemplate> 
</ScrollViewer.Resources> 

<ContentControl Content="{Binding TableCollection}"/> 

</ScrollViewer> 
+1

什麼是你的問題?關於'ContextMenu'的一些事情?還請發佈您的XAML –

+0

@JonathonLee對不起我的壞。不知何故,我錯過了它。是的,我想將ContextMenu添加到DataGrid的每個級別 –

+0

是屬性'Menu'應該是數據網格的上下文菜單? – pushpraj

回答

3

在這裏你去

截圖

screenshot

XAML

<DataGrid Name="dg" 
      SelectedItem="{Binding DataContext.SelectedItem, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor,AncestorType=ItemsControl}}" 
      CanUserAddRows="False" 
      ItemsSource="{Binding}" 
      AutoGenerateColumns="True" 
      Tag="{Binding DataContext, RelativeSource={RelativeSource FindAncestor,AncestorType=ItemsControl}}"> 
    <DataGrid.ContextMenu> 
     <ContextMenu ItemsSource="{Binding PlacementTarget.Tag.Menu, RelativeSource={RelativeSource Self}}" /> 
    </DataGrid.ContextMenu> 

注意到這兩個東西

  • 在數據網格Tag="{Binding DataContext, RelativeSource={RelativeSource FindAncestor,AncestorType=ItemsControl}}"
  • 在上下文菜單

這是一種解決方法爲上下文菜單ItemsSource="{Binding PlacementTarget.Tag.Menu, RelativeSource={RelativeSource Self}}"不是數據網格的可視樹的一部分,從而不能使用直接或者甚至結合到菜單屬性FindAncestor

樣本菜單生成

public CustomTable() 
{ 
    Main = new List<DataTable>(); 
    Main.Add(someRandomTable()); 

    Menu = new List<string>(); 
    Menu.Add("Menu item 1"); 
    Menu.Add("Menu item 2"); 
    Menu.Add("Menu item 3"); 
    Menu.Add("Menu item 4"); 

} 
+0

非常感謝:) –

+0

[Question](http://stackoverflow.com/questions/24735743/mvvm-wpf-validating-datagrid-row-for-autogeneratedcolumns)任何想法?你真的很擅長這個:) –

相關問題