【问题标题】:ListView not updated after adding new items to List将新项目添加到列表后 ListView 未更新
【发布时间】:2015-02-10 08:42:32
【问题描述】:

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

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

有什么方法可以使 List 绑定工作?为什么我用了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 个属性,Value 和 IsChecked,我认为这与这里无关。

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

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

【问题讨论】:

  • 把你的代码Xaml和VM!
  • 这取决于您如何实现 INotifyPropertyChanged。如果您像其他任何属性一样执行此操作,那么它将在整个集合对象本身发生更改时触发,而不是在其元素被修改时触发。
  • 向我们展示您的 c#-Code 和您的列表。你的 XAML 也会很棒。
  • 这对您没有帮助吗? [WPF ListBox 绑定更新][1] [1]: stackoverflow.com/questions/19906911/wpf-listbox-binding-update
  • 听起来您正在视图模型中实现排序。取决于你想要做什么,sorting can be handled by the view.

标签: c# wpf list observablecollection inotifypropertychanged


【解决方案1】:

您的 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 对其进行排序并将排序后的集合绑定到您ListView/ListBox。 如您所见,在 DataContext 初始化之后添加元素会影响 UI 正确排序最后添加的项目(“零”)。

【讨论】:

  • 我确实尝试过,但它对我不起作用,我认为我需要 WPF4.5 中的 IsLiveSortingRequested。我会再试一次。
【解决方案2】:

您需要使用ObservableCollection,因为这会引发一个集合更改事件,您的 wpf ListView 将接收该事件。

怎么样

Public ObservableCollection<object> MyList
{
   get 
   {
      return new ObservableCollection<object>(MySortedList);
   }
}

然后每当您更改排序列表时,都会为 MyList 引发属性更改事件。

这显然取决于您希望如何对列表进行排序,因为您可能需要对ObservableCollection 进行排序,您的问题需要更多信息。

【讨论】:

  • 那么,有没有办法为列表引发集合更改事件?我猜不是?
  • 不。您可以将 1 个列表和 1 个 observablecollection 同步到它,但我认为这不切实际。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-15
相关资源
最近更新 更多