I am losing the selected value when going from one item to the other. My combox box is like this :
<local:ComboBoxEx开发者_开发技巧 x:Name="MYCB"
ItemsSource="{Binding Committente}"
SelectedValue="{Binding CommittenteSelected, Mode=TwoWay}"
SelectionChanged="committente_SelectionChanged"
/>
Committente comes from the ViewModel :
private ObservableCollection<CommittenteV> _Committente;
public ObservableCollection<CommittenteV> Committente
{
get { return _Committente; }
set
{
_Committente = value;
RaisePropertyChanged("Committente");
}
}
in the constructor of my viewmodel, I get my collection done like this :
Committente = ObservableCollectionConverter.GetObservableCollection<CommittenteV>(Service.getList);
Committente.Insert(0, null);
And I add a null Item in order to be able to have an item which symbolises "All Items"
My problem is that going from "real" items to this null item, I do not get CommittenteSelected equal to null but instead the value of the precedent item selected.
If this is not working, how could I decently implement this "all item" featur to feed the combobox of my filters ?
Thanks for your help,
Instead of setting this to null, you could add a static CommittenteV to CommittenteV that returns a "Unspecified/All" instance of CommittenteV. Something akin to:
public static CommittenteV AllCommittente = new CommittenteV { Id = SomeUniqueValue };
Then check against this instead of against null. And when you add it to your list:
Committente.Insert(0, CommittenteV.AllCommittente);
Perhaps give that "AllCommittente" a unique id that will never be used by any of the "real" Committentes? It also allows you to create specific display labels (if you have a property that is used for display value in the combobox) for this AllCommittente entry.
You could also create a child object, of your CommittenteV object, so that when you are checking which object was selected you can use the 'is' operator.
public class AllCommittenteV : CommittenteV, IRepresentAll {}
Committente.Insert(0, new AllCommittenteV());
if (SelectedItem is typeof(AllCommittenteV))
// Use All
else
// Use One
This opens the door for you to be able to create a Processor class, that you can inject into your hosting VM. Throw the processor class into the SelectionChanged method and you can change the entire behavior of the class, by simply changing what Precessor you inject.
Using the interface you have a contract that states anytime this object is represented, to treat that object as if, all was selected.
精彩评论