【发布时间】:2014-07-05 12:00:39
【问题描述】:
我正在开发一个基于 MVVM 的 wpf 应用程序。我所做的只是尝试将嵌套的 DataGrids 与我的 DataTable 绑定。我有类 CustomTable
public class CustomTable : INotifyPropertyChanged
{
public List<DataTable> Main { get; set; }
public CustomTable Child { get; set; }
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));
}
}
}
自定义表由 DataTables 列表 -> Main、SelectedItem 和 CustomTable 类型的子级组成。我正在为这个类实现 INotifyPropertyChanged。
所以我有 ItemsControl,其 DataContext 是此类,并且 ItemSource 绑定到 Main。我的 ItemControl.Template 由 DataGrid 组成。在成功运行时,它不会显示绑定到主列表中每个元素的 DataGrid。现在我将 DataGrid 的 SelectedItem 属性绑定到 CustomTable 的 SelectedItem 属性。我也能做到这一点。现在,当我选择一行并调用 customTable 的 SelectedItem 的设置器时,我正在创建其中包含预定义 Main 的 Child 的新实例(仅供示例,实际逻辑很复杂,与问题无关)。现在我想将我的 Child 绑定到我的 selectesRow 的 RowDetailsTemplate 以便显示嵌套的 dataGrids。这应该是递归的,单击 Child 的 DataGrid 中的一行也应该显示 Child->Child->Main。 我无法做到这一点,并在最近几天尝试没有任何进展。除此之外的任何其他方法也受到欢迎。
编辑
XAML 文件
<Window x:Class="HierDataGrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tvcc="clr-namespace:HierDataGrid"
Title="MainWindow" Height="350" Width="525" xmlns:metro="http://schemas.codeplex.com/elysium">
<Window.Resources>
<DataTemplate x:Key="Nested">
<ItemsControl ItemsSource="{Binding DataContext.Tables, RelativeSource={RelativeSource AncestorType=DataGrid, Mode=FindAncestor}}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<DataGrid CanUserAddRows="False" RowDetailsTemplate="{DynamicResource Nested}" ItemsSource="{Binding Main}" AutoGenerateColumns="True" >
</DataGrid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</Window.Resources>
<ScrollViewer DataContext="{Binding}">
<StackPanel>
<ItemsControl ItemsSource="{Binding Path=TableCollection.Main}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<DataGrid Name="dg" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" CanUserAddRows="False" RowDetailsTemplate="{StaticResource Nested}" ItemsSource="{Binding}" AutoGenerateColumns="True" >
</DataGrid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</ScrollViewer>
【问题讨论】:
-
是
TableCollection是CustomTable类型吗? -
如果您还可以发布预期输出的图片,我们将不胜感激。
标签: c# wpf mvvm datagrid datatable