请参阅下面的我的XAML:基本上我试图创建一个分层数据网格。
<Window x:Class="My.Forms.Wpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Asst="clr-namespace:mY.Data;assembly=Assette.Data"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ObjectDataProvider x:Key="src" ObjectType="{x:Type Asst:CustomerRepo}" ></ObjectDataProvider>
</Window.Resources>
<Grid DataContext="{StaticResource src}">
<DataGrid ItemsSource="{Binding Customers}" AutoGenerateColumns="False" RowDetailsVisibilityMode="Collapsed" >
<DataGrid.Columns>
<DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" />
<DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" />
<DataGridTextColumn Header="Email" Binding="{Binding Email}" />
<DataGridTextColumn Header="Telephone" Binding="{Binding Telephone}" />
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="ShowHideDetails">Details</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<DataGrid ItemsSource="{Binding Orders}" AutoGenerateColumns="False">
<DataGridTextColumn Header="Product Name" Binding="{Binding ProductName}" />
<DataGridTextColumn Header="Quantity" Binding="{Binding Quantity}" />
<DataGridTextColumn Header="Unit Price" Binding="{Binding UnitPrice}" />
<DataGridTextColumn Header="Total Price" Binding="{Binding TotalPrice}" />
</DataGrid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</Grid>
这绑定到内存集合中:
public class CustomerRepo
{
public ObservableCollection<Customer> Customers { get; set; }
//CustomerDataEntities custDb = new CustomerDataEntities();
public CustomerRepo()
{
Customers = new ObservableCollection<Customer>();
Customers.Add(new Customer { FirstName = "John", LastName = "Doe", Email = "aneef@xyz.com", Telephone = "+94773260789" });
Customers.Add(new Customer { FirstName = "Albert", LastName = "Einstein", Email = "eienstain@xyz.com", Telephone = "+46546546" });
Customers.Add(new Customer { FirstName = "Carlos", LastName = "Puyol", Email = "puyol@barca.com", Telephone = "+5465465" });
var customer1 = new Customer { FirstName = "Lionel", LastName = "Messi", Email = "messi@barca.com", Telephone = "+756454686" };
customer1.Orders.Add(new Order { ProductName = "Boot", Quantity = 1, UnitPrice = 100, TotalPrice = 200 });
customer1.Orders.Add(new Order { ProductName = "Soccer Ball", Quantity = 1, UnitPrice = 100, TotalPrice = 200 });
customer1.Orders.Add(new Order { ProductName = "Jersey", Quantity = 1, UnitPrice = 100, TotalPrice = 200 });
Customers.Add(customer1);
var customer2 = new Customer { FirstName = "Gerath", LastName = "Bale", Email = "bale@madrid.com", Telephone = "+12312333" };
customer2.Orders.Add(new Order { ProductName = "Shorts", Quantity = 1, UnitPrice = 100, TotalPrice = 200 });
customer2.Orders.Add(new Order { ProductName = "Guards", Quantity = 1, UnitPrice = 100, TotalPrice = 200 });
customer2.Orders.Add(new Order { ProductName = "Sports Bag", Quantity = 1, UnitPrice = 100, TotalPrice = 200 });
Customers.Add(customer2);
Customers.Add(new Customer { FirstName = "Cristiano", LastName = "Ronaldo", Email = "ronaldo@madrid.com", Telephone = "+234234234" });
Customers.Add(new Customer { FirstName = "Iker", LastName = "Cassilas", Email = "cassilas@madrid.com", Telephone = "+54645645" });
}
}
基本上,当我点击详细信息按钮时,我想要显示详细信息视图,这是另一个网格。如果详细信息网格设置为AutoGenerateColumns = true,则一切正常,但是当我手动设置列详细信息时,它会引发以下异常:
使用ItemsSource时,操作无效。访问和修改 而使用ItemsControl.ItemsSource的元素。
对此有什么想法吗?
答案 0 :(得分:16)
对于内部数据网格,您忘记将列包裹在<DataGrid.Columns>
标记下:
<DataGrid ItemsSource="{Binding Orders}" AutoGenerateColumns="False">
<DataGrid.Columns> <-- This is missing.
<DataGridTextColumn Header="Product Name" Binding="{Binding ProductName}" />
<DataGridTextColumn Header="Quantity" Binding="{Binding Quantity}" />
<DataGridTextColumn Header="Unit Price" Binding="{Binding UnitPrice}" />
<DataGridTextColumn Header="Total Price" Binding="{Binding TotalPrice}" />
</DataGrid.Columns>
</DataGrid>