Set default value in WPF ComboBox

I use the ComboBox ItemsSource property property to display items from a list in a combo box.

Below is the code:

<ComboBox x:Name="Cmb_Tax" ItemsSource="{Binding TaxList}" 
            DisplayMemberPath="ChargeName" SelectedItem="{Binding 
            SelectedTax,UpdateSourceTrigger=PropertyChanged}" IsEditable="True" 
            IsTextSearchEnabled="True" SelectionChanged="Cmb_Tax_SelectionChanged"/>

Classes.Charges _selected_tax = new Classes.Charges();
public Classes.Charges SelectedTax
{
    get
    {
        return _selected_tax;
    }
    set
    {
        _selected_tax = value;
    }
}

List<Classes.Charges> _taxlist = new List<Classes.Charges>();
public List<Classes.Charges> TaxList
{
    get
    {
        return _taxlist;
    }
    set
    {
        _taxlist = value;
        OnPropertyChanged("TaxList");
    }
}

It displays items in the combo box correctly.

TaxList "No Tax"has a specific item that I want to select by default in the combo box. This element can be present at any index in the list (optional first or last element of the list).

I am trying to use the following code to set the property of the selected index in a combo box, but unfortunately it does not work.

TaxList = Classes.Charges.GetChargeList("Tax");
Cmb_Tax.DataContext = this;            
int i = TaxList.FindIndex(x => x.ChargeName == tax_name);
Cmb_Tax.SelectedIndex = i;

The FindIndex () method returns the index "No Tax"correctly, but when I try to assign it a SelectedIndexcombo, it SelectedIndexdoes not change. It remains equal to -1.

Update1

private void Cmb_Tax_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    MessageBox.Show(SelectedTax.ChargeName);
}

Update2 @ElectricRouge

<ComboBox x:Name="Cmb_Tax" ItemsSource="{Binding TaxList, Mode=TwoWay}" 
                      DisplayMemberPath="ChargeName" SelectedItem="{Binding SelectedTax,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
                      IsEditable="True" IsTextSearchEnabled="True" 
                      SelectionChanged="Cmb_Tax_SelectionChanged"/>


Classes.Charges _selected_tax = new Classes.Charges();
        public Classes.Charges SelectedTax
        {
            get
            {
                return _selected_tax;
            }
            set
            {
                _selected_tax = value;
                OnPropertyChanged("SelectedTax");
            }
        }

        ObservableCollection<Classes.Charges> _taxlist = new ObservableCollection<Classes.Charges>();
        public ObservableCollection<Classes.Charges> TaxList
        {
            get
            {
                return _taxlist;
            }
            set
            {
                _taxlist = value;
                OnPropertyChanged("TaxList");
            }
        }

public void Load_Tax(string tax_name = null, Classes.Charges selected_tax = null)
        {
            TaxList = Classes.Charges.GetParticularChargeList("Tax");
            Cmb_Tax.DataContext = this;
            //Cmb_Tax.SelectedValue = tax_name;
            SelectedTax = selected_tax;
            //int i = TaxList.FindIndex(x => x.ChargeName == tax_name);
            //Cmb_Tax.SelectedIndex = i;
        }

, ? .

+4
2

:

ViewModel:

 public MainWindow()
    {
        InitializeComponent();

        var vm = new ViewModel();
        this.DataContext = vm;
        this.Loaded += (o,e) => vm.LoadData();
    }

    public class ViewModel : INotifyPropertyChanged
    {
        private IList<Charges> taxList;
        public ICollectionView TaxList { get; private set; }

        public void LoadData()
        {                
            taxList = Charges.GetChargeList("taxes");

            TaxList = CollectionViewSource.GetDefaultView(taxList);
            RaisePropertyChanged("TaxList");

            TaxList.CurrentChanged += TaxList_CurrentChanged;              

            var noTax = taxList.FirstOrDefault(c => c.ChargeName == "No Tax");
            TaxList.MoveCurrentTo(noTax);
        }

        void TaxList_CurrentChanged(object sender, EventArgs e)
        {
            var currentCharge = TaxList.CurrentItem as Charges;
            if(currentCharge != null)
                MessageBox.Show(currentCharge.ChargeName);
        }



        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

:

<ComboBox x:Name="cboTaxList" 
              ItemsSource="{Binding TaxList}" 
              DisplayMemberPath="ChargeName"
              IsSynchronizedWithCurrentItem="True" />
+3

INotifyCollectionChanged ObservableCollection

ObservableCollection<Classes.Charges> _taxlist = new ObservableCollection<Classes.Charges>();
public ObservableCollection<Classes.Charges> TaxList
{
    get
    {
        return _taxlist;
    }
    set
    {
        _taxlist = value;
        OnPropertyChanged("TaxList");
    }
}

= TwoWay

SelectedItem="{Binding SelectedTax,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
0

Source: https://habr.com/ru/post/1525103/


All Articles