C# 将新项目添加到列表后未更新ListView
我有一个C# 将新项目添加到列表后未更新ListView,c#,wpf,list,observablecollection,inotifypropertychanged,C#,Wpf,List,Observablecollection,Inotifypropertychanged,我有一个ListView绑定到我创建的类的列表。在执行操作时,应该从列表中添加/删除项目,但我的列表视图没有更新,即使我使用了INotifyPropertyChanged 如果我使用ObservableCollection,它可以工作,但我需要对列表进行排序,ObservableCollection不会对WPF4.0进行排序:( 有什么办法可以让列表绑定工作吗?为什么即使我使用了INotifyPropertyChanged,它也不能工作 XAML: 虚拟机: private List_sel
ListView
绑定到我创建的类的列表。在执行操作时,应该从列表中添加/删除项目,但我的列表视图
没有更新,即使我使用了INotifyPropertyChanged
如果我使用ObservableCollection
,它可以工作,但我需要对列表进行排序,ObservableCollection
不会对WPF4.0进行排序:(
有什么办法可以让列表绑定工作吗?为什么即使我使用了INotifyPropertyChanged
,它也不能工作
XAML:
虚拟机:
private List_selectedValues=new List();
公共列表选定值
{
获取{return\u selectedValues;}
设置
{
_selectedValues=值;
OnPropertyChanged();
}
}
private void UnselectValueCommandExecute(CheckBoxItem值)
{
value.IsSelected=false;
选择的值。删除(值);
//OnPropertyChanged(“SelectedValues”);
OnPropertyChanged(“IsAllFilteredValuesSelected”);
}
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(handler!=null)handler(这是新的PropertyChangedEventArgs(propertyName));
}
CheckBoxItem
类包含两个属性,Value
和IsChecked
,我认为这与此处无关
因此,基本上,我有一个按钮,它使用UnselectValueCommandExecute
从列表中删除项目,我应该在UI中看到列表更新,但我没有
调试时,我可以看到已更新的
SelectedValues
列表,但看不到我的UI。您需要使用ObservableCollection
,因为这会引发一个集合更改事件,您的wpf ListView将在该事件中拾取该事件
做什么呢
Public ObservableCollection<object> MyList
{
get
{
return new ObservableCollection<object>(MySortedList);
}
}
公共可观察收集MyList
{
得到
{
返回新的ObservableCollection(MySortedList);
}
}
然后,无论何时更改排序列表,都会引发MyList的属性更改事件
这显然取决于您希望如何对列表进行排序,因为可以对
可观察集合进行排序
您的问题需要更多信息。您的UI中需要一个集合视图源
XAML:
<Window x:Class="WavTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<CollectionViewSource Source="{Binding TestSource}" x:Key="cvs">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Order"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Window.Resources>
<ListView ItemsSource="{Binding Source={StaticResource cvs}}" DisplayMemberPath="Description"/>
</Window>
namespace WavTest
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var vm = new ViewModel();
this.DataContext = vm;
vm.TestSource.Add(new TestItem { Description="Zero", Order=0 });
}
}
public class ViewModel
{
public ObservableCollection<TestItem> TestSource { get; set; }
public ViewModel()
{
TestSource = new ObservableCollection<TestItem>();
TestSource.Add(new TestItem { Description = "Second", Order = 2 });
TestSource.Add(new TestItem { Description = "Third", Order = 3 });
TestSource.Add(new TestItem { Description = "First", Order = 1 });
}
}
public class TestItem
{
public int Order { get; set; }
public string Description { get; set; }
}
}
隐藏的代码:
<Window x:Class="WavTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<CollectionViewSource Source="{Binding TestSource}" x:Key="cvs">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Order"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Window.Resources>
<ListView ItemsSource="{Binding Source={StaticResource cvs}}" DisplayMemberPath="Description"/>
</Window>
namespace WavTest
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var vm = new ViewModel();
this.DataContext = vm;
vm.TestSource.Add(new TestItem { Description="Zero", Order=0 });
}
}
public class ViewModel
{
public ObservableCollection<TestItem> TestSource { get; set; }
public ViewModel()
{
TestSource = new ObservableCollection<TestItem>();
TestSource.Add(new TestItem { Description = "Second", Order = 2 });
TestSource.Add(new TestItem { Description = "Third", Order = 3 });
TestSource.Add(new TestItem { Description = "First", Order = 1 });
}
}
public class TestItem
{
public int Order { get; set; }
public string Description { get; set; }
}
}
名称空间测试
{
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
var vm=new ViewModel();
this.DataContext=vm;
Add(newtestItem{Description=“Zero”,Order=0});
}
}
公共类视图模型
{
公共ObservableCollection TestSource{get;set;}
公共视图模型()
{
TestSource=新的ObservableCollection();
Add(newtestItem{Description=“Second”,Order=2});
Add(newtestItem{Description=“Third”,Order=3});
Add(newtestItem{Description=“First”,Order=1});
}
}
公开课测验
{
公共整数顺序{get;set;}
公共字符串说明{get;set;}
}
}
说明:
<Window x:Class="WavTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<CollectionViewSource Source="{Binding TestSource}" x:Key="cvs">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Order"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Window.Resources>
<ListView ItemsSource="{Binding Source={StaticResource cvs}}" DisplayMemberPath="Description"/>
</Window>
namespace WavTest
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var vm = new ViewModel();
this.DataContext = vm;
vm.TestSource.Add(new TestItem { Description="Zero", Order=0 });
}
}
public class ViewModel
{
public ObservableCollection<TestItem> TestSource { get; set; }
public ViewModel()
{
TestSource = new ObservableCollection<TestItem>();
TestSource.Add(new TestItem { Description = "Second", Order = 2 });
TestSource.Add(new TestItem { Description = "Third", Order = 3 });
TestSource.Add(new TestItem { Description = "First", Order = 1 });
}
}
public class TestItem
{
public int Order { get; set; }
public string Description { get; set; }
}
}
ObservableCollection
会按预期引发PropertyChanged
事件,但您无法对其进行排序。
因此,您需要CollectionView
对其进行排序,并将排序后的集合绑定到您的ListView
/ListBox
。
如您所见,在DataContext
初始化之后添加元素会影响对最后添加的项进行排序的UI(“零”)正确。将您的代码放入Xaml和VM!这取决于您如何实现INotifyPropertyChanged。如果您像其他任何属性一样执行此操作,那么它将在整个集合对象本身发生更改时触发,而不是在其元素被修改时触发。将您的c#代码与列表一起显示给我们。您的Xaml也将非常好。这对y有帮助吗ou?[WPF列表框绑定更新][1][1]:听起来像是在viewmodel中实现排序。这取决于您尝试执行的操作。那么,有没有办法为list引发collection changed事件?我想没有?没有。您可以将1个list和1个observablecollection同步到它,但我认为这不实用。我尝试过,但对我不起作用,我认为我没有eded IsLiveSorting已从WPF4.5请求。我将重试此操作。