简体   繁体   中英

C# WPF MVVM ComboBox SelectedItem

I know that it's been here a million times but I don't know what's wrong in my code. I tried everything but the ComboBox is not binding SelectedItem correctly. Here is my complete sandbox solution. You can also find it on GitHub ( https://github.com/LukasNespor/ComboBoxBinding ).

BindableBase.cs

public class BindableBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public virtual void RaisePropertyChanged(string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

ContactModel.cs

public class ContactModel : BindableBase
{
    private int _Id;
    public int Id
    {
        get { return _Id; }
        set
        {
            _Id = value;
            RaisePropertyChanged(nameof(Id));
        }
    }

    private string _Name;
    public string Name
    {
        get { return _Name; }
        set
        {
            _Name = value;
            RaisePropertyChanged(nameof(Name));
        }
    }

    private string _Phone;
    public string Phone
    {
        get { return _Phone; }
        set
        {
            _Phone = value;
            RaisePropertyChanged(nameof(Phone));
        }
    }

    public override bool Equals(object obj)
    {
        if (obj != null || !(obj is ContactModel))
            return false;

        return ((ContactModel)obj).Id == this.Id;
    }

    public override string ToString()
    {
        return $"{Name} {Phone}";
    }
}

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
    ...>
<Grid>
    <ComboBox Width="200" Height="23"
              SelectedItem="{Binding ViewModel.SelectedContact}" ItemsSource="{Binding ViewModel.Contacts}" />
</Grid>

MainWindows.xaml.cs

public partial class MainWindow : Window
{
    public MainViewModel ViewModel { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;

        ViewModel = new MainViewModel();
        ViewModel.SelectedContact = new ContactModel
        {
            Id = 2,
            Name = "Some Guy",
            Phone = "+123456789"
        };
    }
}

MainViewModel.cs

public class MainViewModel : BindableBase
{
    public List<ContactModel> Contacts
    {
        get
        {
            return new List<ContactModel>
            {
                new ContactModel() {Id = 1, Name = "John Doe", Phone = "+166666333" },
                new ContactModel() {Id = 2, Name = "Some Guy", Phone = "+123456789" }
            };
        }
    }

    private ContactModel _SelectedContact;
    public ContactModel SelectedContact
    {
        get { return _SelectedContact; }
        set
        {
            _SelectedContact = value;
            RaisePropertyChanged(nameof(SelectedContact));
        }
    }
}

You need do Sync the list with the current selected item:

1.Xaml:

<ComboBox ItemsSource="{Binding Contacts}" SelectedItem="{Binding SelectedContact}" IsSynchronizedWithCurrentItem="True" />

2.ViewModel:

public ObservableCollection<ContactModel> Contacts { get; set; }

    public MainViewModel()
    {
        _model = new Model {Name = "Prop Name" };
        Contacts = new ObservableCollection<ContactModel>
        {
            new ContactModel {Id = 1, Name = "John Doe", Phone = "+166666333"},
            new ContactModel {Id = 2, Name = "Some Guy", Phone = "+123456789"}
        };
        SelectedContact = Contacts[0];
    }

    private ContactModel _SelectedContact;

    public ContactModel SelectedContact
    {
        get { return _SelectedContact; }
        set
        {
            _SelectedContact = value;
            OnPropertyChanged(nameof(SelectedContact));
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM