WPF数据网格:ItemsSource正在使用时操作无效
本文关键字:操作 无效 数据网 数据 网格 ItemsSource WPF | 更新日期: 2023-09-27 17:57:41
请参阅下面的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。
你知道吗?
对于内部dataGrid,您忘记了将列包装在<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>