WPF:两个 DataGrid,相同的 ItemsSource,一个是只读的,Bug?
我有一个 WPF 应用程序,其中有两个共享相同 ItemsSource 的 DataGrid。当我将 DataGrid 的 IsReadOnly 属性之一设置为 true 时,我失去了向另一个 DataGrid 添加记录的能力。我仍然可以编辑第二个数据网格的内容,但只是无法添加记录。
这是故意的吗?有办法解决这个问题吗?我可以对 DataGrid 使用 IsEnabled="False",但随后我就失去了在其中滚动的能力。
设置如下:
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;
}
}
}
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.
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.
Here is the setup:
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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为发生的事情是
IsReadOnly
属性通过persons
的 DefaultView 使DataGrid
只读,并且因为这个 DefaultView 将是相同的对于您的两个DataGrid
,两者都失去了添加新行的能力。然而,两者都不会变成只读(正如您在问题中所说),所以我不确定这是否是一个错误或所需的行为。
我也不确定幕后发生了什么导致此行为,但您可以通过调试器验证 CollectionView 是否相同(因为 CollectionView 属性是私有的)。以下三个陈述是正确的。
您可以通过将
List
更改为ObservableCollection
并使用单独的ListViewCollection's
来让它按照您喜欢的方式工作对于您的DataGrid
I think what's going on is that the
IsReadOnly
property is making theDataGrid
readonly through the DefaultView forpersons
, and since this DefaultView will be the same for both of yourDataGrid'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.
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
You can get it to work the way you like by changing the
List
to anObservableCollection
and use separateListViewCollection's
for yourDataGrid's