【问题标题】:Updating Listview after adding/removing item添加/删除项目后更新列表视图
【发布时间】:2015-04-04 13:02:02
【问题描述】:

如何在 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");
    }
}

【问题讨论】:

    标签: c# listview windows-store-apps


    【解决方案1】:

    您在使用 ObservableCollection 时不需要添加 INotifyCollectionChanged - 它已经实现了所需的接口。

    您的代码应该可以工作,但可能存在一些其他问题:

    • 检查您的 ListView 的 DataContext(在这种情况下是其父级)是否设置正确 - 应该设置它,以便“ListView 会找到”Persons 属性,

    • 还要检查 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 绑定 - 您没有定义属性 Persons 的 setter em>.

    编辑:

    编辑后,我可以看到问题出在哪里 - 您正在为 ListViewButton 设置单独的 DataContext - 按钮是添加到 它自己的 集合 - 不同于 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>
    

    【讨论】:

    • 我认为 DataContext 设置正确,当我在其声明中将对象添加到列表时,我可以看到它们显示出来。将项目添加到列表中也可以,我在调试器中检查了它。我在上一篇文章中编辑了我的代码,这基本上就是我们让它工作所需的全部内容,但由于某种原因它没有。我不知道出了什么问题,或者我只是不明白什么?
    • @MrCrow 我添加了一个编辑。我认为它应该有助于解决问题。
    • @MrCrow 请注意,还有其他可能更好的设置 DataContext 的方法,您可以为整个页面、网格、代码或 xaml 设置它。请注意不要在需要时创建几个模型。祝你好运。
    • 我明白了,但这里还有一个问题:如果我想让我的列表显示在多个页面上怎么办?是否可以声明上下文并在它们之间共享?
    • @MrCrow 这不是问题 - 您可以将模型应用程序范围声明为静态资源,或应用程序类中的变量,然后在每个页面上,您可以在构造函数或 xaml 中设置数据上下文。有多种方法可以做到。
    猜你喜欢
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 2015-05-09
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多