如何在WPF中刷新列表

本文关键字:刷新 列表 WPF | 更新日期: 2023-09-27 18:05:43

private ObservableCollection<classType>_colls;
public ObservableCollection<classType> Colls
{
   get {return _colls;}
   set { _colls = value; OnPropertyChanged("Colls");}
}
public string _name;
public string Name
{
  get { return _name; }
  set { _name= value; OnPropertyChanged("Name"); }
}
public string _Id;
public string Id
{
    get { return _Id; }
    set { _Id= value; OnPropertyChanged("Id"); }
}

我有一个List控件,ItemsSourceObservableCollection绑定。

编辑值后,集合被刷新,但视图的属性不能更改。

classType包含2个属性Name &Id和扩展INotifyPropertyChanged。但是Name属性的值在View中不受影响。

视图模型:

public class ViewModelBase : INotifyPropertyChanged
{
    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
}
public class classType : ViewModelBase
{
    public string _Name;
    public string Name
    {
        get { return _Name; }
        set { _Name = value; OnPropertyChanged("Name"); }
    }
    public string _Id;
    public string Id
    {
        get { return _Id; }
        set { _Id = value; OnPropertyChanged("Id"); }
    }
}

XAML视图:

<ListBox ItemsSource="{Binding Colls,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <StackPanel>
        <TextBlock
          ToolTip="{Binding Name, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
          TextTrimming="WordEllipsis"
          Text="{Binding Path=Name, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
      </StackPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

如何在WPF中刷新列表

不知道你哪里出错了。它需要完整的代码(视图和视图模型)来找到根本原因。让我根据你的代码给出一个工作示例,它将帮助你调试自己。

MainWindow.xaml

<Grid>
    <ListBox ItemsSource="{Binding Colls,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock ToolTip="{Binding Name, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" TextTrimming="WordEllipsis" Text="{Binding Path=Name, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <Button Click="Button_Click" Width="100" Height="50" Content="Click Me" />
</Grid>

MainWindow.xaml.cs

    public MainWindow()
    {
        InitializeComponent();
        Colls = new ObservableCollection<MyClass>();
        Colls.Add(new MyClass()
        {
            Id = "1", Name = "aaa"
        });
        Colls.Add(new MyClass()
        {
            Id = "2",
            Name = "bbb"
        });
        Colls.Add(new MyClass()
        {
            Id = "3",
            Name = "ccc"
        });
        this.DataContext = this;
    }
    private ObservableCollection<MyClass> _colls;
    public ObservableCollection<MyClass> Colls
    {
        get { return _colls; }
        set { _colls = value; }
    }
    private int i = 1;
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Colls[0].Name = "Value changed for " + i++ + " time(s).";
    }

MyClass.cs

public class MyClass : INotifyPropertyChanged
{       
    public string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value; OnPropertyChanged("Name"); }
    }
    public string _Id;
    public string Id
    {
        get { return _Id; }
        set { _Id = value; OnPropertyChanged("Id"); }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propname)
    {
        if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propname));
    }
}

希望对你有帮助。

如您所知,当ViewModel绑定正确时,数据绑定不需要手动刷新命令。

你在classType中的属性可以很好地触发PropertyChanges,但是collection不会将其内部触发的事件告诉外部。所以一个只知道外部事件的Listview不知道变化。为了解决这个问题,ObservableCollection需要将其包含的属性更改冒泡到CollectionChanged事件。

我建议您使用ObservableCollectionEx

  • Nuget包链接:https://www.nuget.org/packages/ObservableCollectionEx/

安装上面的包,只是替换ObservableCollectionObservableCollectionEx在你的ViewModel类,然后所有设置。