[英]Updating Listview after adding/removing item
How can I refresh ListView after adding item to collection in Windows Store app? 将项目添加到Windows Store应用程序中的集合后,如何刷新ListView? Adding items to list works fine, but Listview doesn't refresh.
将项目添加到列表可以正常工作,但Listview不会刷新。 I was trying to implement INotifyCollectionChanged , but what exactly should I do to make it work?
我正在尝试实现INotifyCollectionChanged ,但是究竟应该怎么做才能使其起作用?
edit: XAML file 编辑: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# code: 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");
}
}
You don't need to add INotifyCollectionChanged when you are using ObservableCollection - it already implements needed interfaces. 使用ObservableCollection时不需要添加INotifyCollectionChanged-它已经实现了所需的接口。
Your code should work, though there may be some other problems: 您的代码应该可以运行,尽管可能还有其他问题:
check if the DataContext of your ListView (its parent in this case) is properly set - it should be set, so that 'ListView would find' Persons property, 检查是否正确设置了ListView的DataContext (在本例中为其父级)-应该对其进行设置,以便“ ListView可以找到” Persons属性,
also check if the ItemTemplate is properly set - example: 还检查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>
in your case there is no need that your ListView's ItemsSource uses TwoWay binding - you are not defining a setter ofr the property Persons . 在您的情况下,您的ListView的ItemsSource不需要使用TwoWay绑定-您不需要定义Persons属性的设置器。
EDIT: 编辑:
After your edit, I can see where is the problem - you are setting separate DataContext for your ListView and Button - the button is adding to its own collection - different than ListView is bound to. 编辑之后,我可以看到问题出在哪里-您正在为ListView和Button设置单独的DataContext-按钮将添加到其自己的集合中-与ListView绑定到的集合不同。 Take a short test - set the DataContext of both to same staticresource:
进行简短测试-将两者的DataContext设置为相同的staticresource:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.