c# - 将新项目添加到列表后,ListView 未更新

标签 c# wpf list observablecollection inotifypropertychanged

我有一个 ListView 绑定(bind)到我创建的类的列表。在执行操作时,它应该从列表中添加/删除项目,但我的 ListView 没有更新,即使我使用了 INotifyPropertyChanged

如果我使用 ObservableCollection,它可以工作,但我需要对列表进行排序,而 ObservableCollection 不会为 WPF4.0 进行排序:(

有什么方法可以使列表绑定(bind)工作?为什么即使我使用了 INotifyPropertyChanged 它也不起作用?

XAML:

<ListView BorderThickness="0" ItemsSource="{Binding SelectedValues, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}" Padding="5">
                    <ListView.View>
                        <GridView ColumnHeaderContainerStyle="{StaticResource myHeaderStyle}">
                            <GridViewColumn DisplayMemberBinding="{Binding Value}"></GridViewColumn>

虚拟机:

    private List<CheckBoxItem> _selectedValues = new List<CheckBoxItem>();

    public List<CheckBoxItem> SelectedValues
            {
                get { return _selectedValues; }
                set
                {
                    _selectedValues = value;
                    OnPropertyChanged();
                }
            }

    private void UnselectValueCommandExecute(CheckBoxItem value)
            {
                value.IsSelected = false;
                SelectedValues.Remove(value);
                //OnPropertyChanged("SelectedValues");
                OnPropertyChanged("IsAllFilteredValuesSelected");
            }

public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

CheckBoxItem 类包含 2 个属性,ValueIsChecked,我认为这与这里无关。

基本上,我有一个按钮,它使用 UnselectValueCommandExecute 从列表中删除项目,我应该在 UI 中看到列表更新,但我没有。

当我调试时,我可以看到 SelectedValues 列表已更新,但我的 UI 没有。

最佳答案

您的 UI 中需要一个 CollectionViewSource

XAML:

<Window x:Class="WavTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
        xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"        
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <CollectionViewSource Source="{Binding TestSource}" x:Key="cvs">
            <CollectionViewSource.SortDescriptions>
                <scm:SortDescription PropertyName="Order"/>
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>
    </Window.Resources>
    <ListView ItemsSource="{Binding Source={StaticResource cvs}}" DisplayMemberPath="Description"/>
</Window>

背后的代码:

namespace WavTest
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var vm = new ViewModel();
            this.DataContext = vm;

            vm.TestSource.Add(new TestItem { Description="Zero", Order=0 });
        }
    }

    public class ViewModel 
    {
        public ObservableCollection<TestItem> TestSource { get; set; }

        public ViewModel()
        {
            TestSource = new ObservableCollection<TestItem>();
            TestSource.Add(new TestItem { Description = "Second", Order = 2 });
            TestSource.Add(new TestItem { Description = "Third", Order = 3 });
            TestSource.Add(new TestItem { Description = "First", Order = 1 });
        }
    }

    public class TestItem
    {
        public int Order { get; set; }
        public string Description { get; set; }
    }
}

解释:

ObservableCollection 如您所料引发了 PropertyChanged 事件,但您无法对其进行排序。 因此,您需要 CollectionView 对其进行排序并将排序后的集合绑定(bind)到您的 ListView/ListBox。 如您所见,在 DataContext 初始化后添加一个元素会影响 UI 正确排序最后添加的项目(“零”)。

关于c# - 将新项目添加到列表后,ListView 未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28427296/

相关文章:

c# - 如何为 Azure 函数使用自定义计时器?

c# - 保存文件对话框初始目录

c# - System.Text.Json.JsonException : A possible object cycle was detected

list - 在 Python 中移动列表的最佳方法?

python - 根据另一个列表随机替换字典列表中的元素

c# - 将额外参数传递给事件处理程序?

c# - DataGrid 中的偏移行

c# - 如何将此 DataGrid MouseLeftButtonUp 绑定(bind)重写为 MVVM?

c# - AutomationElement 获取真实类型

c - 我应该如何使用链接列表创建地址簿?