WPF:两个DataGrid,相同的ItemsSource,一个IsReadOnly,Bug? [英] WPF: Two DataGrids, same ItemsSource, One IsReadOnly, Bug?
问题描述
我有一个WPF应用程序,其中有两个共享相同ItemsSource的DataGrid。当我将DataGrid的IsReadOnly属性之一设置为true时,将无法将记录添加到另一个DataGrid。我仍然可以编辑第二个数据网格的内容,但是不能添加记录。
I have a WPF application with two DataGrids that share the same ItemsSource. When I set one of the DataGrid's IsReadOnly property to true, I lose the ability to add records to the other DataGrid. I can still edit the contents of the second datagrid, but just cannot add records.
这是故意的吗?有办法解决吗?我可以对DataGrid使用IsEnabled = False,但是然后我无法在其中滚动。
Is this intended? Is there way around this? I could use IsEnabled="False" for the DataGrid, but I then lose the ability to scroll in it.
这里是设置:
XAML:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<DataGrid Name="dgA" Grid.Row="0" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="FirstName" Binding="{Binding Path=FirstName}" />
<DataGridTextColumn Header="LastName" Binding="{Binding Path=LastName}" />
</DataGrid.Columns>
</DataGrid>
<DataGrid Name="dgB" Grid.Row="1" AutoGenerateColumns="False" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="FirstName" Binding="{Binding Path=FirstName}" />
<DataGridTextColumn Header="LastName" Binding="{Binding Path=LastName}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
C#:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<Person> persons = new List<Person>();
persons.Add(new Person() { FirstName = "Bob", LastName = "Johnson" });
persons.Add(new Person() { FirstName = "John", LastName = "Smith" });
dgA.ItemsSource = persons;
dgB.ItemsSource = persons;
}
class Person
{
public Person() { }
public string FirstName
{
get;
set;
}
public string LastName
{
get;
set;
}
}
}
推荐答案
我认为这是怎么回事, IsReadOnly
属性通过 DataGrid的
都是相同的,因此两者都失去了添加新行的能力。
I think what's going on is that the IsReadOnly
property is making the DataGrid
readonly through the DefaultView for persons
, and since this DefaultView will be the same for both of your DataGrid's
, both looses the ability to add new rows.
但是两者都不是只读的(正如您在问题中所说),所以我不确定这是错误还是所需的行为。
Both doesn't become readonly however (as you said in your question) so I'm not sure if this is a bug or a desired behavior.
我也不确定导致此问题的幕后原因,但是您可以通过调试器验证CollectionView的相同(因为CollectionView属性是私有的)。以下三个语句为真
I'm also not sure what's going on behind the scenes here that causes this behavior but you can verify that the CollectionView's are the same through the debugger (since the CollectionView property is private). The following three statements come out as true
dgA.Items.CollectionView == CollectionViewSource.GetDefaultView(persons) // true
dgB.Items.CollectionView == CollectionViewSource.GetDefaultView(persons) // true
dgA.Items.CollectionView == dgB.Items.CollectionView // true
通过将 List
更改为 ObservableCollection
并为您的 DataGrid的
You can get it to work the way you like by changing the List
to an ObservableCollection
and use separate ListViewCollection's
for your DataGrid's
public MainWindow()
{
InitializeComponent();
ObservableCollection<Person> persons = new ObservableCollection<Person>();
persons.Add(new Person() { FirstName = "Bob", LastName = "Johnson" });
persons.Add(new Person() { FirstName = "John", LastName = "Smith" });
dgA.ItemsSource = new ListCollectionView(persons);
dgB.ItemsSource = new ListCollectionView(persons);
}
这篇关于WPF:两个DataGrid,相同的ItemsSource,一个IsReadOnly,Bug?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!