Combobox selecteditem if default "all" value should be -1

i have a c# WPF project that contains a combobox. i have several items in the "selection", if i select any of those, then the value is correct (0 for the first item, 1 for the 2nd item and so fourth) how can i add the first item as "all", and if user selects "all" then the value is "-1" ? right now "all" is the first item, so it sets it to "0" value

thanks for anyone who can assist

Answers


-1 value states that nothing is selected try using SelectedIndex-1 as your value. If your reference SelectedIndex from xaml then some converter on binding will be required.


instead of using the SelectedIndex use the SelectedItem and check if its the "All" value.

Example:

the ViewModel

public class MyViewModel
{
   public ObservableCollection<string> TheItems { get; set;}
   public string TheSelectedItem { get; set; }

   public MyViewModel()
   {
       TheItems = new ObservableCollection<string>();
       TheItems.Add("All");
       //Adding all the other value

   }

   public void SomeMethod()
   {
       if(TheSelectedItem=="All")
       {
          //Do whatever needs to be done 
       }
   }
}

the View

<ListView ItemsSource="{Binding TheItems}" SelectedItem="{Binding TheSelectedItem}"/>

Need Your Help

How to get list of all Azure VMs in Powershell

powershell azure virtual-machine

I'm attempting to get a list of all my Azure VMs in Powershell.

How do I generate new vectors based on the results of ifelse statements in r?

r

I'm trying to generate two new variables based on the values of an extant variable. It's a very simple operation but I can't seem to get it to function correctly and on all values of a vector in r....