在 adding/removing 项之后更新列表视图
Updating Listview after adding/removing item
在 Windows Store 应用程序中将项目添加到集合后如何刷新 ListView?将项目添加到列表工作正常,但 Listview 不刷新。我正在尝试实施 INotifyCollectionChanged,但我究竟应该怎么做才能让它发挥作用?
编辑:XAML 文件
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView HorizontalAlignment="Left" Height="437" Margin="10,120,0,0" VerticalAlignment="Top" Width="593" ItemsSource="{Binding Persons, Mode=TwoWay}" Background="#FF5D5D5D">
<ListView.DataContext>
<Model:School/>
</ListView.DataContext>
</ListView>
<Button Content="Button" HorizontalAlignment="Left" Margin="7,64,0,0" VerticalAlignment="Top" Command="{Binding AddCommand, Mode=OneWay}">
<Button.DataContext>
<Model:School/>
</Button.DataContext>
</Button>
</Grid>
C#代码:
class School
{
private ObservableCollection<string> _persons = new ObservableCollection<string>()
{
"Name1", "Name2", "Name3"
};
public ObservableCollection<string> Persons
{
get { return _persons; }
}
private ICommand _addCommand;
public ICommand AddCommand
{
get
{
return this._addCommand ??
(this._addCommand = new RelayCommand(Add));
}
}
private void Add()
{
this._persons.Add("Name");
}
}
当您使用 ObservableCollection 时,您不需要添加 INotifyCollectionChanged - 它已经实现了所需的接口。
您的代码应该可以工作,但可能还有一些其他问题:
检查您的 ListView(在本例中为它的父项)的 DataContext 是否设置正确 - 它应该被设置,以便 'ListView would find' 人 属性,
还要检查 ItemTemplate 是否设置正确 - 示例:
<ListView Name="myList" ItemsSource="{Binding Persons}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Surname}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
在您的情况下,您的 ListView 的 ItemsSource 不需要使用 TwoWay 绑定 - 您没有定义setter 的 属性 人。
编辑:
编辑后,我可以看出问题出在哪里 - 您正在为 ListView 和 [=26= 设置单独的 DataContext ]Button - 按钮正在添加到 它自己的 集合 - 不同于绑定到 ListView。进行简短测试 - 将两者的 DataContext 设置为相同的静态资源:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.Resources>
<Model:School x:Key="mySchool"/>
</Grid.Resources>
<ListView HorizontalAlignment="Left" VerticalAlignment="Stretch" Margin="10,120,0,0" ItemsSource="{Binding Persons}" Background="#FF5D5D5D"
DataContext="{StaticResource mySchool}"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="7,64,0,0" VerticalAlignment="Top" Command="{Binding AddCommand, Mode=OneWay}"
DataContext="{StaticResource mySchool}">
</Button>
</Grid>
在 Windows Store 应用程序中将项目添加到集合后如何刷新 ListView?将项目添加到列表工作正常,但 Listview 不刷新。我正在尝试实施 INotifyCollectionChanged,但我究竟应该怎么做才能让它发挥作用?
编辑:XAML 文件
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView HorizontalAlignment="Left" Height="437" Margin="10,120,0,0" VerticalAlignment="Top" Width="593" ItemsSource="{Binding Persons, Mode=TwoWay}" Background="#FF5D5D5D">
<ListView.DataContext>
<Model:School/>
</ListView.DataContext>
</ListView>
<Button Content="Button" HorizontalAlignment="Left" Margin="7,64,0,0" VerticalAlignment="Top" Command="{Binding AddCommand, Mode=OneWay}">
<Button.DataContext>
<Model:School/>
</Button.DataContext>
</Button>
</Grid>
C#代码:
class School
{
private ObservableCollection<string> _persons = new ObservableCollection<string>()
{
"Name1", "Name2", "Name3"
};
public ObservableCollection<string> Persons
{
get { return _persons; }
}
private ICommand _addCommand;
public ICommand AddCommand
{
get
{
return this._addCommand ??
(this._addCommand = new RelayCommand(Add));
}
}
private void Add()
{
this._persons.Add("Name");
}
}
当您使用 ObservableCollection 时,您不需要添加 INotifyCollectionChanged - 它已经实现了所需的接口。
您的代码应该可以工作,但可能还有一些其他问题:
检查您的 ListView(在本例中为它的父项)的 DataContext 是否设置正确 - 它应该被设置,以便 'ListView would find' 人 属性,
还要检查 ItemTemplate 是否设置正确 - 示例:
<ListView Name="myList" ItemsSource="{Binding Persons}"> <ListView.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Name}"/> <TextBlock Text="{Binding Surname}"/> </StackPanel> </DataTemplate> </ListView.ItemTemplate> </ListView>
在您的情况下,您的 ListView 的 ItemsSource 不需要使用 TwoWay 绑定 - 您没有定义setter 的 属性 人。
编辑:
编辑后,我可以看出问题出在哪里 - 您正在为 ListView 和 [=26= 设置单独的 DataContext ]Button - 按钮正在添加到 它自己的 集合 - 不同于绑定到 ListView。进行简短测试 - 将两者的 DataContext 设置为相同的静态资源:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.Resources>
<Model:School x:Key="mySchool"/>
</Grid.Resources>
<ListView HorizontalAlignment="Left" VerticalAlignment="Stretch" Margin="10,120,0,0" ItemsSource="{Binding Persons}" Background="#FF5D5D5D"
DataContext="{StaticResource mySchool}"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="7,64,0,0" VerticalAlignment="Top" Command="{Binding AddCommand, Mode=OneWay}"
DataContext="{StaticResource mySchool}">
</Button>
</Grid>