wpf DataGrid 不使用 ObservableCollection 更新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23127205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:33:51  来源:igfitidea点击:

DataGrid does not updating with ObservableCollection

c#wpfmvvmdatagridobservablecollection

提问by kLeR1k

I'm trying to learn MVVM (Using MVVMLight Toolkit). But i'm stuck.

我正在尝试学习 MVVM(使用 MVVMLight 工具包)。但我被困住了。

In ViewModel have an ObservableCollection

在 ViewModel 中有一个 ObservableCollection

private ObservableCollection<Phone> _phoneNumbers;

public ObservableCollection<Phone> PhoneNumbers
{
    get { return _phoneNumbers; }
    private set
    {
        _phoneNumbers = value;
    }
}

In ViewModel constructor fill it in such way PhoneNumbers = new ObservableCollection<Phone>(Guest.Person.Phones);

在 ViewModel 构造函数中以这种方式填充它 PhoneNumbers = new ObservableCollection<Phone>(Guest.Person.Phones);

In view have

鉴于有

<DataGrid x:Name="PhoneNumbersDataGrid" HorizontalAlignment="Left" Grid.Column="3" Grid.Row="11" VerticalAlignment="Top" Height="86" Width="auto" ItemsSource="{Binding PhoneNumbers, Mode=OneWay}" AutoGenerateColumns="False" IsReadOnly="True">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding PhoneNumber}" Header="Phone Number" IsReadOnly="True"/>
        <DataGridTemplateColumn Header="Delete">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button Command="{Binding Guest.DeletePhoneCommand, Mode=OneWay, Source={StaticResource Locator}}">Delete</Button>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

In DeletePhoneCommand I'm trying to change PhoneNumberse.g.

在 DeletePhoneCommand 我试图改变PhoneNumbers例如

PhoneNumbers = new ObservableCollection<Phone>();
RaisePropertyChanged("PhoneNumbers");

Collection became empty but datagrid displays filled collection without any changes. "get" of Collection fires only when view is loading. Even when i RaisePropertyChanged("PhoneNumbers")it doesn't fire.

集合变为空,但数据网格显示已填充的集合而没有任何更改。只有在加载视图时才会触发 Collection 的“get”。即使我 RaisePropertyChanged("PhoneNumbers")不火。

What am I doing wrong?

我究竟做错了什么?

回答by Sajeetharan

If you want to notify the changes to your collection you should use Mode = TwoWay

如果你想通知你的收藏的变化,你应该使用 Mode = TwoWay

Two way will enable any change in object is reflected to the UI and any change in UI is reflected in the object.

两种方式将启用对象的任何更改反映到 UI 和 UI 的任何更改反映在对象中。

<DataGrid x:Name="PhoneNumbersDataGrid" HorizontalAlignment="Left" Grid.Column="3" Grid.Row="11" VerticalAlignment="Top" Height="86" Width="auto" ItemsSource="{Binding PhoneNumbers, Mode=TwoWay}" AutoGenerateColumns="False" IsReadOnly="True">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding PhoneNumber,Mode=TwoWay}" Header="Phone Number" IsReadOnly="True"/>
                <DataGridTemplateColumn Header="Delete">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Command="{Binding Guest.DeletePhoneCommand, Mode=OneWay, Source={StaticResource Locator}}">Delete</Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

回答by dnr3

If I'm not mistaken, the binding is broken when you reinitialize the collection. to fix this you will have to work on the already initialized collection. e.g :

如果我没记错的话,当您重新初始化集合时绑定会被破坏。要解决此问题,您必须处理已经初始化的集合。例如:

clear the list using the

使用

PhoneNumbers.Clear()

PhoneNumbers.Clear()

to add to the list, using

添加到列表中,使用

PhoneNumbers.Add(item)

PhoneNumbers.Add(item)

you don't need to raise the property changed event to update the list in the UI when you're using ObservableCollection. You will need it if you change a property of the item on the list.

当您使用 ObservableCollection 时,您不需要引发属性更改事件来更新 UI 中的列表。如果您更改列表中项目的属性,您将需要它。

回答by mohsen.asmand

at first clear the list with

首先清除列表

Clear()

then add item

然后添加项目

PhoneNumbers.Add(item)