首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >绑定到共享源的ReadWrite & ReadOnly数据网格,导致ReadWrite不正确的ReadWrite?

绑定到共享源的ReadWrite & ReadOnly数据网格,导致ReadWrite不正确的ReadWrite?
EN

Stack Overflow用户
提问于 2012-12-11 03:53:00
回答 1查看 246关注 0票数 4

我发现了一些奇怪的东西:我有一个表单,其中包含两个绑定到同一个集合的数据网格。根据Xaml中数据网格的顺序,行为会有所不同。

这按预期工作(存在用于添加的额外行):

代码语言:javascript
复制
<DockPanel>
    <DockPanel DockPanel.Dock="Right">
        <Label Content="ReadOnlyView" DockPanel.Dock="Top"/>
        <DataGrid ItemsSource="{Binding Persons}" IsReadOnly="True" />
    </DockPanel>
    <DockPanel>
        <Label Content="EditorView" DockPanel.Dock="Top" />
        <DataGrid ItemsSource="{Binding Persons}" IsReadOnly="False" CanUserAddRows="True" />
    </DockPanel>
</DockPanel>

以这种方式排列xaml让我感到困惑(没有额外的行可供添加)

代码语言:javascript
复制
<DockPanel>
    <DockPanel>
        <Label Content="EditorView" DockPanel.Dock="Top" />
        <DataGrid ItemsSource="{Binding Persons}" IsReadOnly="False" CanUserAddRows="True" />
    </DockPanel>
    <DockPanel DockPanel.Dock="Right">
        <Label Content="ReadOnlyView" DockPanel.Dock="Top"/>
        <DataGrid ItemsSource="{Binding Persons}" IsReadOnly="True" />
    </DockPanel>
</DockPanel>

下面是我用来做这个的虚拟ViewModel:

代码语言:javascript
复制
public class PersonsViewModel
{
    public PersonsViewModel()
    {
        Persons = new ObservableCollection<Person>
                    {
                        new Person {Name = "Johan"},
                        new Person {Name = "Dave"},
                    };
    }

    public ObservableCollection<Person> Persons { get; private set; }
}

public class Person
{
    public string Name { get; set; }
}

我的问题是,这种行为的原因是什么?

EN

回答 1

Stack Overflow用户

发布于 2012-12-11 19:51:21

一个很好的问题,约翰!我的猜测是,由于您没有显式地为其提供CollectionViewSource,因此由DataGrid自动生成的cvs将在两者之间共享,因为您引用的是相同的源。

因此,当您发出两个IsReadOnly赋值并作为共享源时,最后一个设置将获胜,这两个DataGrid都会向您显示相同的效果。

为了证实我的猜测,我使用了这段代码,当您提供显式的CollectionViewSource使用时,DataGrids的行为就像您所期望的那样。

代码语言:javascript
复制
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <CollectionViewSource Source="{Binding Persons}" x:Key="cvs1" />
        <CollectionViewSource Source="{Binding Persons}" x:Key="cvs2" />
    </Window.Resources>
    <DockPanel>
        <DataGrid ItemsSource="{Binding Source={StaticResource ResourceKey=cvs1}}" IsReadOnly="False" CanUserAddRows="True" />
        <DataGrid ItemsSource="{Binding Source={StaticResource ResourceKey=cvs2}}" IsReadOnly="True" />
    </DockPanel>
</Window>

编辑:进一步的测试表明,这种行为可以说是奇怪的!我无法解释为什么这会产生三个只读DG

代码语言:javascript
复制
<DataGrid ItemsSource="{Binding Persons}" IsReadOnly="True" />
<DataGrid ItemsSource="{Binding Persons}" IsReadOnly="False" />
<DataGrid ItemsSource="{Binding Persons}" IsReadOnly="True" />

但这会产生交替的只读和可编辑DG:

代码语言:javascript
复制
<DataGrid ItemsSource="{Binding Persons}" IsReadOnly="True" />
<DataGrid ItemsSource="{Binding Persons}" IsReadOnly="False" />
<DataGrid ItemsSource="{Binding Persons}" IsReadOnly="True" />
<DataGrid ItemsSource="{Binding Persons}" IsReadOnly="False" />

因此,我想上面的CVS最适合描述为解决这种奇怪的行为,这样你就可以实现你真正想要的东西。

编辑2:在更多的true false组合之后,我注意到的唯一一致的事情是,如果DataGrid中的最后一个IsReadOnly设置为True,则所有其他DataGrids都是只读的。但如果最后一项设置为false,则所有其他DataGrids将根据其自己的IsReadOnly设置进行操作。此行为可能是由于此MSDN bit造成的

代码语言:javascript
复制
If a conflict exists between the settings at the DataGrid, column,
or cell levels, a value of true takes precedence over a value of false.
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13808143

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档