【问题标题】:Windows 10 Development: How to refresh ListView whenever there is a change in the items inside ListView?Windows 10 开发:如何在 ListView 内的项目发生变化时刷新 ListView?
【发布时间】:2016-05-09 08:55:04
【问题描述】:

我对数据绑定的概念非常陌生,我认为我并不完全理解它。我有一个名为Project 的类,其中LinkedList 类型为ToDo 作为其属性之一。当我导航到Project 的一个实例时,我将在ListView 中显示ToDo 类型的LinkedList。我创建的函数允许我更改LinkedList 中的节点序列(上移、下移)并删除所选节点(删除)。我希望ListViewLinkedList 发生变化时刷新(向上移动、向下移动或删除)。但是,我无法做到这一点。这是我的代码:(并非所有部分都包括在内)

页面的 XAML:

<ListView x:Name="myListView" ItemsSource="{Binding Source={StaticResource ToDos}, Mode=TwoWay}">    
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <CheckBox x:Name="myCheckBox" 
                          Content="{Binding ToDoTitle, Mode=TwoWay}" 
                          IsChecked="{Binding IsCompleted, Mode=TwoWay}">                         
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>      
</ListView>

用于数据模型的 C#:

public class ToDo : INotifyPropertyChanged
{
    private string toDoTitle;
    private bool isCompleted;
    public event PropertyChangedEventHandler PropertyChanged = delegate { };
    public string ToDoTitle { get { return this.toDoTitle; } set { this.toDoTitle = value; this.OnPropertyChanged(); } }
    public bool IsCompleted { get { return this.isCompleted; } set { this.isCompleted = value; this.OnPropertyChanged(); } }

    public void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        // Raise the PropertyChanged event, passing the name of the property whose value has changed.
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

}

public class Projects : INotifyPropertyChanged
{
    private LinkedList<ToDo> toDos;

    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    public LinkedList<ToDo> ToDos { get { return this.toDos; } set { this.toDos = value; this.OnCollectionChanged(); } }

    public Projects()
    {
        ToDos = new LinkedList<ToDo>();

    }
    public void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        // Raise the PropertyChanged event, passing the name of the property whose value has changed.
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

谢谢。

【问题讨论】:

    标签: c# xaml listview windows-10-universal


    【解决方案1】:

    首先,我建议您阅读有关 MVVM 的内容,并尝试遵循一些基本教程,例如 this one

    一开始您可以使用MVVM Light 来避免自己管理 INotifyPropertyChanged(但了解 MVVM 灯在后台的工作原理真的很好)。

    回到您的问题,您当前的代码仅在您设置了完整的待办事项列表时才会发出通知。如果您想了解列表中的任何更改(查看项目何时添加/删除/更新),您可能正在寻找 ObservableCollection,而不是 LinkedList。

    希望对你有帮助。

    【讨论】:

    • 我同意 - 最快的解决方案是将 LinkedList&lt;ToDo&gt; 替换为 ObservableCollection&lt;ToDo&gt;。它实现了与INotifyPropertyChanged类似的接口:INotifyCollectionChanged
    • @ManfredRadlwimmer 感谢您的回答,但我希望我可以保留链表,因为我使用了几个链表的方法,我想保留它们。在实施 INotifyPropertyChanged/NotifyCollectionChanged 以使其工作时,有什么方法可以保留链表?
    • 不是,不是。您使用LinkedList 有什么特别的原因吗? ObservableCollection 中不存在的某些功能?在大多数情况下,使用任何类型的 List 的方法都可以与常用接口之一(IEnumerableICollectionIList 等)一起工作。
    • @ManfredRadlwimmer 所以,我尝试了这个:继续使用linkedlist,添加了一个从linkedlist导入值的observablecollection,并将这个observablecollection绑定到我的listview。但是,使用断点分析应用程序何时运行,我意识到修改链表(添加/删除/更新)不会触发 OnPropertyChanged,因为不会触发 get 和 set 访问器。所以,我尝试在修改完成后直接调用 OnPropertyChanged(添加/删除/更新)。 OnProperyChanged 函数已执行,但 UI 未更改。
    • @ManfredRadlwimmer 执行 OnPropertyChanged 函数时,应该刷新 ListView 吧?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    相关资源
    最近更新 更多