0
votes

I have one textBox and one combobox in wpf usercontrol. ComboxBox is binded to ICollectionView (CurencyList) which populates Currency Pairs like GBP/EUR,CLP/EUR,USD/EUR,EUR/USD etc. Whenever the user writes in textbox e.g. EUR the combobox should get filtered and display the dropdownlist with EUR as the second currency.

enter image description here

For this I have used like:

 public string Currency
    {
        get { 

            return _criteriaType.currency; }
        set
        {
            if (_criteriaType.currency != value)
            {
                _criteriaType.currency = value.EmptyOrWhiteSpaceAsNull();
                base.OnPropertyChanged("Currency");
                CurrencyList.Filter = new Predicate<object>(Contains);

            }
        }
    }



public bool Contains(object de)
    {
        CurrencyPair o = de as CurrencyPair;
        if (Currency != null || Currency == string.Empty)
        {
           return (o.name.Substring(3, 4).ToLower().Contains(Currency.ToLower()));              
        }
        else
        {
            IsOpen = false;
            OnPropertyChanged("IsOpen");               
            return false;
        }
    }

CurrencyList is coming from a webservice:

 private ICollectionView GetCurrencyPair()
    {

        strCurrencyPair.arg0 = (Currency != string.Empty && Currency != null) ? Convert.ToString(Currency).ToUpper() : string.Empty;
        string[][] cPair = ServiceLocator.Resolve<IWebServiceRepository>().BusinessWebService.getCurrenyPairs(strCurrencyPair);

        foreach (string[] item in cPair)
        {
            IList.Add(new CurrencyPair() { name = (Convert.ToString(item[0]).ToUpper() + "/" + Convert.ToString(item[1]).ToUpper()) });

        }
      return  CurrencyList =CollectionViewSource.GetDefaultView(IList);

    }

Filtering is working fine. But when the user deletes the currency from textbox with backward arrow key from keyboard, the combobox filtered to nothing i.e. dropdownlist is empty. How to overcome this problem. kindly suggest?

2

2 Answers

1
votes

you need to update your collection view through the text change event

private void OnTextChanged(object sender, Eventargs e) {
  var vm = yourViewModelOrWhatEver;
  ((ICollectionView)vm.CurrencyList).Refresh();
}

or set the filter predicate once and fire only the refresh on currency change

public void ctor() {
  CurrencyList.Filter = new Predicate<object>(Contains);
}

public string Currency {
  get { return _criteriaType.currency; }
  set {
    if (_criteriaType.currency == value) {
      return;
    }
    _criteriaType.currency = value.EmptyOrWhiteSpaceAsNull();
    base.OnPropertyChanged("Currency");
    CurrencyList.Refresh(); // refresh/filter the collection view
  }
}

hope that helps

EDIT

you say

Filtering is working fine. But when the user deletes the currency from textbox with backward arrow key from keyboard, the combobox filtered to nothing i.e. dropdownlist is empty. How to overcome this problem. kindly suggest?

then you must change your predicate function to this one

public bool Contains(object de)
{
    CurrencyPair o = de as CurrencyPair;
    if (Currency != null || Currency == string.Empty) {
      // Currency == string.Empty should also true
      return (Currency == string.Empty) || (o.name.Substring(3, 4).ToLower().Contains(Currency.ToLower()));
    } else {
      IsOpen = false;
      OnPropertyChanged("IsOpen");               
      return false;
    }
}
0
votes

I would assume the issue lies with this statement:

string[][] cPair = ServiceLocator.Resolve().BusinessWebService.getCurrenyPairs(strCurrencyPair);

what is the return value from that service if you pass string.empty for strCurrencyPair as you will currently be doing once the textbox is empty from backspacing?