在WPF ComboBox中设置默认值

时间:2014-02-03 19:14:33

标签: c# .net wpf

我正在使用ComboBox ItemsSource属性绑定来显示从List到组合框的项目。

以下是代码:

<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");
    }
}

它正确显示组合框中的项目。

TaxList "No Tax"中有一个特定项目,我想在组合框中默认选中它。此项目可以出现在列表中的任何索引处(不需要列表的第一项或最后一项)。

我正在尝试使用以下代码来设置组合框的选定索引属性,但遗憾的是它无法正常工作。

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

方法FindIndex()正确返回"No Tax"的索引,但当我尝试将其分配给组合的SelectedIndex时,SelectedIndex不会更改。它保持在-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;
        }

知道为什么必须这样做吗? 另外,请建议在组合框中显示默认值的任何其他方法。

2 个答案:

答案 0 :(得分:3)

这是一个工作样本:

视图模型:

 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" />

答案 1 :(得分:0)

列表未实现INotifyCollectionChanged将其设为ObservableCollection

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

尝试设置Mode = TwoWay

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