Windows 10 开发:如何在ListView里面的item发生变化的时候刷新ListView?
Windows 10 Development: How to refresh ListView whenever there is a change in the items inside ListView?
我对数据绑定的概念很陌生,我认为我没有完全理解它。我有一个名为 Project
的 class,其属性之一是类型为 ToDo
的 LinkedList
。当我导航到 Project
的一个实例时,我将在 ListView
中显示 ToDo
类型的 LinkedList
。我创建了一些函数,允许我更改 LinkedList
中节点的顺序(上移、下移)并删除选定的节点(删除)。我希望 ListView
在 LinkedList
发生变化时刷新(上移、下移或删除)。但是,我无法做到这一点。这是我的代码:(并非所有部分都包括在内)
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));
}
}
谢谢。
首先,我建议您阅读有关 MVVM 的内容,并尝试遵循一些基础教程,例如 this one。
您可以使用 MVVM Light 来避免一开始自己管理 INotifyPropertyChanged(但了解 MVVM light 在幕后的工作原理真的很好)。
回到您的问题,您当前的代码仅在您设置完整的 ToDos 列表时才会发出通知。如果你想知道列表中的任何变化(当一个项目是 add/remove/update 时),你可能正在寻找一个 ObservableCollection,而不是 LinkedList。
希望对您有所帮助。
我对数据绑定的概念很陌生,我认为我没有完全理解它。我有一个名为 Project
的 class,其属性之一是类型为 ToDo
的 LinkedList
。当我导航到 Project
的一个实例时,我将在 ListView
中显示 ToDo
类型的 LinkedList
。我创建了一些函数,允许我更改 LinkedList
中节点的顺序(上移、下移)并删除选定的节点(删除)。我希望 ListView
在 LinkedList
发生变化时刷新(上移、下移或删除)。但是,我无法做到这一点。这是我的代码:(并非所有部分都包括在内)
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));
}
}
谢谢。
首先,我建议您阅读有关 MVVM 的内容,并尝试遵循一些基础教程,例如 this one。
您可以使用 MVVM Light 来避免一开始自己管理 INotifyPropertyChanged(但了解 MVVM light 在幕后的工作原理真的很好)。
回到您的问题,您当前的代码仅在您设置完整的 ToDos 列表时才会发出通知。如果你想知道列表中的任何变化(当一个项目是 add/remove/update 时),你可能正在寻找一个 ObservableCollection,而不是 LinkedList。
希望对您有所帮助。