WPF:两个 DataGrid,相同的 ItemsSource,一个 IsReadOnly,Bug? [英] WPF: Two DataGrids, same ItemsSource, One IsReadOnly, Bug?

查看:21
本文介绍了WPF:两个 DataGrid,相同的 ItemsSource,一个 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 通过 DefaultView 成为只读的persons,并且由于此 DefaultView 对于您的两个 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 并为您的 使用单独的 ListViewCollection's 来使其按照您喜欢的方式工作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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆