我正在尝试将我的ViewModel绑定到我的ComboBox.我有像这样定义的ViewModel类:
class ViewModel { public ViewModel() { this.Car= "VW"; } public string Car{ get; set; } }
我在Window_Load中将此ViewModel设置为DataContext,如:
private void Window_Loaded(object sender, RoutedEventArgs e) { this.DataContext = new CarModel(); }
然后在我的xaml中,我这样做将我的ComboBox绑定到此ViewModel.我想在我的ComboBox中默认显示"VW":
Mazda VW Audi
我有两个问题:
如何将组合框中选择的默认值设置为"VW"(一旦表单加载,它应在组合框中显示"VW").
取而代之的上述XAML中设置ComboBoxItems一样,怎么我把它在我的视图模型,然后在组合框加载这些?
谢谢,
更新:到目前为止,我设法实现这一点,但我在ViewModel c-tor中得到如下错误:
namespace MyData { class ViewModel { public ViewModel() { this.Make = ""; this.Year = 1; this.DefaultCar = "VW"; //this is where I get error 'No string allowed' } public IEnumerableCars { get { var cars = new Car[] { new Car{Model="Mazda"}, new Car{Model="VW"}, new Car{Model="Audi"} }; DefaultCar = cars.FirstOrDefault(car => car.Model == "VW"); } } public string Make { get; set; } public int Year { get; set; } public Car DefaultCar { get; set; } } class Car { public string Model { get; set; } } }
E-Bat.. 8
正如你想要实现MVVM一样,如果你开始考虑在你的应用程序中代表Cars的对象,那将会好得多:
public class ViewModel { public Car SelectedCar{ get; set; } public IEnumerableCars{ get { var cars = YOUR_DATA_STORE.Cars.ToList(); SelectedCar = cars.FirstOrDefault(car=>car.Model == "VW"); return cars; } } } public class Car { public string Model {get;set;} public string Make { get; set; } public int Year { get; set; } }
你的Xaml:
FUR10N.. 5
默认值:如果设置viewModel.Car = "VW"
,则它将在组合框中自动选择该项目。为此,您需要在INotifyPropertyChanged
设置Car
之前实施或设置DataContext
。
INotifyPropertyChanged实现可能类似于:
class ViewModel : INotifyPropertyChanged { private string car; public ViewModel() { this.Car = "VW"; this.Cars = new ObservableCollection() { "Mazda", "VW", "Audi" }; } public string Car { get { return this.car; } set { if (this.car != value) { this.car = value; OnPropertyChanged(); } } } public ObservableCollection Cars { get; } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } }
2.绑定ItemsSource
和SelectedItem
。
您还可以设置ComboBox.ItemTemplate
源或视图是否比仅显示字符串更复杂:
在视图模型中,只需添加一个list属性:
public ObservableCollectionCars { get; set; }
它不是必须的,ObservableCollection
但是只要您更改集合,该类型就会自动更新UI。
正如你想要实现MVVM一样,如果你开始考虑在你的应用程序中代表Cars的对象,那将会好得多:
public class ViewModel { public Car SelectedCar{ get; set; } public IEnumerableCars{ get { var cars = YOUR_DATA_STORE.Cars.ToList(); SelectedCar = cars.FirstOrDefault(car=>car.Model == "VW"); return cars; } } } public class Car { public string Model {get;set;} public string Make { get; set; } public int Year { get; set; } }
你的Xaml:
默认值:如果设置viewModel.Car = "VW"
,则它将在组合框中自动选择该项目。为此,您需要在INotifyPropertyChanged
设置Car
之前实施或设置DataContext
。
INotifyPropertyChanged实现可能类似于:
class ViewModel : INotifyPropertyChanged { private string car; public ViewModel() { this.Car = "VW"; this.Cars = new ObservableCollection() { "Mazda", "VW", "Audi" }; } public string Car { get { return this.car; } set { if (this.car != value) { this.car = value; OnPropertyChanged(); } } } public ObservableCollection Cars { get; } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } }
2.绑定ItemsSource
和SelectedItem
。
您还可以设置ComboBox.ItemTemplate
源或视图是否比仅显示字符串更复杂:
在视图模型中,只需添加一个list属性:
public ObservableCollectionCars { get; set; }
它不是必须的,ObservableCollection
但是只要您更改集合,该类型就会自动更新UI。