【问题标题】:ReadWrite & ReadOnly datagrid with binding to shared source, resulting in ReadWrite not proper ReadWrite?ReadWrite & ReadOnly 数据网格绑定到共享源,导致 ReadWrite 不正确 ReadWrite?
【发布时间】:2012-12-10 19:52:59
【问题描述】:

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

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

<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 让我感到困惑(没有额外的添加行)

<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:

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

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

【问题讨论】:

    标签: wpf binding wpfdatagrid readonly-attribute


    【解决方案1】:

    一个很好的问题,约翰!我的猜测是,由于您没有明确提供 CollectionViewSource,因此 DataGrid 自动生成的 cvs 在两者之间共享,因为您指的是同一来源。

    因此,当您发出两个 IsReadOnly 分配并作为共享源时,最后一个设置获胜,两个 DataGrid 显示相同的效果。

    为了证实我的猜测,我使用了此代码,当您提供明确的 CollectionViewSource 以供使用时,DataGrids 的行为与您预期的一样。

    <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

    <DataGrid ItemsSource="{Binding Persons}" IsReadOnly="True" />
    <DataGrid ItemsSource="{Binding Persons}" IsReadOnly="False" />
    <DataGrid ItemsSource="{Binding Persons}" IsReadOnly="True" />
    

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

    <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:在更多的真假组合之后,我注意到的唯一一致的事情是,如果DataGrid 中的最后一个IsReadOnly 设置为True,则所有其他DataGrids 将变为只读。但是如果最后一个设置为 false,那么所有其他 DataGrid 的行为都将根据它们自己的 IsReadOnly 设置进行。这种行为可能是由于MSDN bit

    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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-03
      • 2016-10-05
      • 2012-06-30
      • 1970-01-01
      • 2011-11-25
      • 2013-03-20
      • 1970-01-01
      相关资源
      最近更新 更多