在 C# Windows 通用应用程序中将 ViewModel List<T> 绑定到列表框
Bind ViewModel List<T> to Listbox in C# Windows Universal App
我有一个列表框,我想在项目添加到列表时更新它。我知道我需要绑定列表框。我试图遵循这个 question/answer。
我有一个处理列表的 ViewModel:
namespace TESTS
{
public class ViewModel : INotifyPropertyChanged
{
private List<Cars> _listCars;
public List<Cars> listCars
{
get
{
return _listCars;
}
set
{
if (_listCars == value)
{
return;
}
this.RaisePropertyChanged("Message");
_listCars = value;
this.RaisePropertyChanged("Message");
}
}
public ViewModel()
{
listCars = new List<Cars>();
}
protected void RaisePropertyChanged(string propertyName)
{
Debug.WriteLine("Property Changed");
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
这是class辆汽车:
public class Cars: INotifyPropertyChanged
{
public string model{ get; set; }
public string year{ get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
所以我将列表框绑定到我的 Viewmodel 中的 属性 路径,即 listCars。
<ListBox .... ItemsSource="{Binding listCars}">
所以在我的 Main.xaml.cs 中。我单击按钮并添加项目。它不会被添加到列表框,即使它绑定到视图模型上的列表。
public sealed partial class MainPage : Page
{
public static ViewModel vm = new ViewModel();
public MainPage()
{
this.InitializeComponent();
this.DataContext = vm;
}
private void button_Click(object sender, RoutedEventArgs e)
{
Cars x = new Cars();
x.model = "Ford";
x.Year = "1998";
vm.listCars.Add(x);
}
}
我希望我解释得足够好。我的 ViewModel 实现有问题吗?我是 MVVM 的新手。请帮忙。
使用 ObservableCollection<T>
,而不是 List<T>
。前者旨在与 MVVM 一起使用,后者则不是。您将自动收到所有通知。使用 List<T>
是可行的,但是您必须编写更多的代码,并且性能会 很多 更差,尤其是对于大集合。只是不要这样做。
如果您在构造函数中创建集合,将其分配给只读 属性 并且永远不要更改其实例(这是您应该这样做的方式),您不会甚至不需要实施 INPC。
实施 INPC 时,您需要在更改 属性 后调用 RaisePropertyChanged
一次,并且 属性 名称已已更改,而不是随机的无关字符串。
我有一个列表框,我想在项目添加到列表时更新它。我知道我需要绑定列表框。我试图遵循这个 question/answer。
我有一个处理列表的 ViewModel:
namespace TESTS
{
public class ViewModel : INotifyPropertyChanged
{
private List<Cars> _listCars;
public List<Cars> listCars
{
get
{
return _listCars;
}
set
{
if (_listCars == value)
{
return;
}
this.RaisePropertyChanged("Message");
_listCars = value;
this.RaisePropertyChanged("Message");
}
}
public ViewModel()
{
listCars = new List<Cars>();
}
protected void RaisePropertyChanged(string propertyName)
{
Debug.WriteLine("Property Changed");
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
这是class辆汽车:
public class Cars: INotifyPropertyChanged
{
public string model{ get; set; }
public string year{ get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
所以我将列表框绑定到我的 Viewmodel 中的 属性 路径,即 listCars。
<ListBox .... ItemsSource="{Binding listCars}">
所以在我的 Main.xaml.cs 中。我单击按钮并添加项目。它不会被添加到列表框,即使它绑定到视图模型上的列表。
public sealed partial class MainPage : Page
{
public static ViewModel vm = new ViewModel();
public MainPage()
{
this.InitializeComponent();
this.DataContext = vm;
}
private void button_Click(object sender, RoutedEventArgs e)
{
Cars x = new Cars();
x.model = "Ford";
x.Year = "1998";
vm.listCars.Add(x);
}
}
我希望我解释得足够好。我的 ViewModel 实现有问题吗?我是 MVVM 的新手。请帮忙。
使用
ObservableCollection<T>
,而不是List<T>
。前者旨在与 MVVM 一起使用,后者则不是。您将自动收到所有通知。使用List<T>
是可行的,但是您必须编写更多的代码,并且性能会 很多 更差,尤其是对于大集合。只是不要这样做。如果您在构造函数中创建集合,将其分配给只读 属性 并且永远不要更改其实例(这是您应该这样做的方式),您不会甚至不需要实施 INPC。
实施 INPC 时,您需要在更改 属性 后调用
RaisePropertyChanged
一次,并且 属性 名称已已更改,而不是随机的无关字符串。