我有嵌套的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>
什麼是你的問題?關於'ContextMenu'的一些事情?還請發佈您的XAML –
@JonathonLee對不起我的壞。不知何故,我錯過了它。是的,我想將ContextMenu添加到DataGrid的每個級別 –
是屬性'Menu'應該是數據網格的上下文菜單? – pushpraj