当前位置:  开发笔记 > 编程语言 > 正文

WPF绑定ComboBox到我的ViewModel

如何解决《WPF绑定ComboBox到我的ViewModel》经验,为你挑选了2个好方法。

我正在尝试将我的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 IEnumerable Cars
        {
            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 IEnumerable Cars{ 
                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.绑定ItemsSourceSelectedItem



您还可以设置ComboBox.ItemTemplate源或视图是否比仅显示字符串更复杂:

    
        
            
        
    

在视图模型中,只需添加一个list属性:

public ObservableCollection Cars { get; set; }

它不是必须的,ObservableCollection但是只要您更改集合,该类型就会自动更新UI。



1> E-Bat..:

正如你想要实现MVVM一样,如果你开始考虑在你的应用程序中代表Cars的对象,那将会好得多:

    public class ViewModel
    {    
            public Car SelectedCar{ get; set; }
            public IEnumerable Cars{ 
                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:




2> FUR10N..:

    默认值:如果设置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.绑定ItemsSourceSelectedItem



您还可以设置ComboBox.ItemTemplate源或视图是否比仅显示字符串更复杂:

    
        
            
        
    

在视图模型中,只需添加一个list属性:

public ObservableCollection Cars { get; set; }

它不是必须的,ObservableCollection但是只要您更改集合,该类型就会自动更新UI。

推荐阅读
依然-狠幸福
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有