1

只使用一个 DataGrid 就可以了。

但在我的示例代码中,我定义了一个复选框(窗口左上角)来切换 ListView 项中 DataGrids 的 ReadOnly 属性的状态。ListView DataGrids 的只读状态正在改变,但有副作用:

  • 在 ListBox 之前定义的 DataGrid 也在更改其 ReadOnly 状态。
  • 在 ListBox 中定义的 DataGrids 也遵循在 ListView 中定义的 Datagrids 的 ReadOnly 属性的状态。

在人们讲述重复后更新:只是为了让人们了解。由于使用了相同的 ItemsSource,会出现此问题。引擎盖下将使用相同的 CollectionView。与 CollectionView 相关的每个功能,例如排序顺序或 IsReadOnly 属性都将在具有相同 ItemsSource(引擎盖下的相同 CollectionView)的每个控件上受到影响。

为什么?

要重现问题...只需执行普通的 WPF 应用程序并应用 2 处修改:

  1. 将 MainWindow xaml 更改为:

    <ScrollViewer>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height ="Auto"></RowDefinition>
                <RowDefinition Height ="Auto"></RowDefinition>
                <RowDefinition Height ="Auto"></RowDefinition>
                <RowDefinition Height ="Auto"></RowDefinition>
                <RowDefinition Height ="Auto"></RowDefinition>
                <RowDefinition Height ="Auto"></RowDefinition>
            </Grid.RowDefinitions>
    
            <CheckBox Name="CheckBox"></CheckBox>
    
            <TextBlock Grid.Row="1" Text="{Binding Families.Count}"></TextBlock>
            <!--<Button Click="ButtonBase_OnClick"></Button>-->
    
            <DataGrid Grid.Row="2" ItemsSource="{Binding Familiy1.Persons}"
                    IsReadOnly="True">
            </DataGrid>
    
            <DataGrid Grid.Row="3" ItemsSource="{Binding Familiy1.Persons}"
                    IsReadOnly="false">
            </DataGrid>
    
            <ListBox Grid.Row="4" HorizontalContentAlignment="Stretch" 
                    ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
                    ItemsSource="{Binding Families}" Background="Khaki">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <GroupBox Header="ListBox">
                            <DataGrid ItemsSource="{Binding Persons}">
                            </DataGrid>
                        </GroupBox>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
    
            <ListView Grid.Row="5" 
                    ItemsSource="{Binding Families}"
                    Margin="0,24,0,0" Background="Thistle"
                    ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <GroupBox>
                            <GroupBox.Header>
                                <TextBlock Text="ListView"></TextBlock>
                            </GroupBox.Header>
    
                            <DataGrid ItemsSource="{Binding Persons}"
                                    Background="Red" AutoGenerateColumns="True"
                                    ColumnWidth="Auto"
                                      IsReadOnly="{Binding IsChecked , ElementName=CheckBox}">
                            </DataGrid>
                        </GroupBox>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
    </ScrollViewer>
    

  2. 此外,添加以下 MainWindowModel.cs 类:

    using System.Collections.ObjectModel;
    using System.ComponentModel;
    
    
    namespace WpfDataGridIsReadOnlyBug
    { 
    public class Person : INotifyPropertyChanged
    {
        private int _age;
        private string _name;
    
        public string Name
        {
            get { return _name; }
            set { _name = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Name")); }
        }
    
        public int Age
        {
            get { return _age; }
            set { _age = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Age")); }
        }
    
        public Person()
        {
    
        }
    
        public Person(string name, int age)
        {
            Name = name;
            Age = age;
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    }
    
    public class Family : INotifyPropertyChanged
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Name")); }
        }
    
        public Family()
        {
    
        }
    
        public Family(string name)
        {
            Name = name;
        }
        public ObservableCollection<Person> Persons { get; } = new ObservableCollection<Person>();
    
        public event PropertyChangedEventHandler PropertyChanged;
    
    }
    
    public class MainWindowModel
    {
        public ObservableCollection<Family> Families { get; } = new ObservableCollection<Family>();
    
        public Family Familiy1
        {
            get { return Families[0]; }
        }
    
        public MainWindowModel()
        {
            Family family = new Family("Family 1");
            family.Persons.Add(new Person("Eric", 50));
            family.Persons.Add(new Person("Isabel Lucas", 35));
            Families.Add(family);
    
            family = new Family("Family 2");
            family.Persons.Add(new Person("Candice Swanopel", 25));
            Families.Add(family);
        }
    }
    }
    
4

0 回答 0