WPF:两个 DataGrid,相同的 ItemsSource,一个是只读的,Bug?

发布于 2024-11-19 19:33:17 字数 1940 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过潦 2024-11-26 19:33:17

我认为发生的事情是 IsReadOnly 属性通过 persons 的 DefaultView 使 DataGrid 只读,并且因为这个 DefaultView 将是相同的对于您的两个 DataGrid,两者都失去了添加新行的能力。

然而,两者都不会变成只读(正如您在问题中所说),所以我不确定这是否是一个错误或所需的行为。

我也不确定幕后发生了什么导致此行为,但您可以通过调试器验证 CollectionView 是否相同(因为 CollectionView 属性是私有的)。以下三个陈述是正确的。

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

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);
}

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.

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文