0

I have the following question: In my view model I have a list with an object that has a property Name and Value, both strings. I want to bind the list to a combo box in my view, but I want to display only the elements that have a certain name. For the list:

Name&nbsp&nbsp&nbsp&nbspValue
foo&nbsp&nbsp&nbsp&nbspaaa
bar&nbsp&nbsp&nbsp&nbspbbbb
foo&nbsp&nbsp&nbsp&nbspccc

I want to display in the combobox only the elements that have the name foo, aaa and ccc. The catch here is that I want to do the filter in the view and not in the codebehind or the viewmodel.

ViewCode:

<ComboBox IsEditable="True" VerticalAlignment="Top" 
      HorizontalAlignment="Left" Width="150" Margin="60,60,0,0" 
      ItemsSource="{Binding Elements}"  
      SelectedValue="{Binding Value}" SelectedValuePath="Value" 
      DisplayMemberPath="Value" />

ViewModel Code:

private List<CustomChartElement> elements;
public List<CustomChartElement> Elements
    {
        get { return this.elements; }
    }
darkArk
  • 144
  • 1
  • 11
  • post the relevant code and XAML. Use a `CollectionView`. – Federico Berasategui Jul 23 '13 at 14:32
  • @HighCore. My code is not relevant for this question. – darkArk Jul 23 '13 at 14:47
  • "questions must demonstrate a minimum understanding of the subject...." as per the StackOverflow guidelines. At least you need to show what you already tried. – Federico Berasategui Jul 23 '13 at 14:48
  • @HighCore Because I have used the right terminology eg: view, viewmodel, codebehind, binding etc. I believe I have demonstrated that I have "a minimum understanding of the subject". However if you insist I shall post the code that you ask. – darkArk Jul 23 '13 at 14:53

1 Answers1

1

You could just add a property that does the filtering and bind to that one instead of the list you exposed.

If you have several comboboxes and each one requires a sublist of the base list filtered according to different filter logics you will have to think about implementing each list in your model as it's own property in the view. You may consider to also encapsule your model inside a container class which exposes the different lists to not clutter up your base model.

I found an idea to do so called Command Binding. Here is an example with passing a parameter. It may be what you were looking for.

Since it requires ICommandSource, you might want to have a look at the article here on how to implement ICommandSource for Combobox it.

Community
  • 1
  • 1
Marwie
  • 3,177
  • 3
  • 28
  • 49
  • That list will be binded to multiple comboboxes in the same view. – darkArk Jul 23 '13 at 14:39
  • If I understand you, you do only want to apply the filter for one bound control and not for all the others, right? Why don't you just add a second property with filtered Items and one which does not and bind correspondingly. – Marwie Jul 23 '13 at 14:45
  • Each of my comboboxes will be bound to a subset of that list, based on the Name property. – darkArk Jul 23 '13 at 14:48
  • How about implementing a solution with a binding that passes a Parameter (the filter)? [Try this](http://zamjad.wordpress.com/2010/01/19/command-binding-with-parameter-passing/) – Marwie Jul 23 '13 at 15:06
  • This looks very interesting. I will try it and will return with feedback. Thank you. – darkArk Jul 23 '13 at 15:18
  • Unfortunately the solution from the example doesn't work because the combobox doesn't implement ICommandSource. – darkArk Jul 24 '13 at 07:33