How to set combobox by default to no selected item

Sarah 186 Reputation points
2022-04-22T23:29:13.867+00:00

Hi, I bind a list(ObservableCollection) to a ComboBox. There is always one item selected. I want that by default no item is selected. How can I do this?

 <ComboBox Width="200" Height="25" ItemsSource="{Binding CountryView}" DisplayMemberPath="Country" SelectedItem="{Binding SelectedCountry}"/>
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,670 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Hui Liu-MSFT 38,191 Reputation points Microsoft Vendor
    2022-04-25T06:28:08.683+00:00

    Case 1 : If your CountryView is an ObservableCollection, the Combobox below has no items selected by default.

    MainWindow.xaml:

      <Grid>  
            <ComboBox Width="200" Height="25" ItemsSource="{Binding CountryView}" DisplayMemberPath="Country"  
                      SelectedItem="{Binding SelectedCountry}"/>  
        </Grid>  
    

    MainWindow.xaml.cs:

    using System;  
    using System.Collections.ObjectModel;  
    using System.Windows;  
    using System.Windows.Data;  
      
    namespace ComboboxBindCollectionView  
    {  
      public partial class MainWindow : Window  
      {  
        public MainWindow()  
        {  
          InitializeComponent();  
          DataContext = new ViewModel();  
        }  
      }  
      public class ViewModel  
      {  
        public ObservableCollection<MyCountry> CountryView { get; set; }  
         
        public MyCountry SelectedCountry { get;set;}  
        public ViewModel()  
        {  
          CountryView = new ObservableCollection<MyCountry>();  
          CountryView.Add(new MyCountry() { Country = "Beijing"});  
          CountryView.Add(new MyCountry() { Country = "NewYork" });  
          CountryView.Add(new MyCountry() { Country = "Shanghai"});  
          CountryView.Add(new MyCountry() { Country = "Paris" });  
         
        }  
      }  
      public class MyCountry  
      {  
        public string Country { get; set; }  
      }  
    }  
    

    Case 2: If your CountryView is a CollectionViewSource, the Combobox below has no items selected by default.
    MainWindow.xaml:

     <Window.Resources>  
            <CollectionViewSource x:Key="CountryView"/>  
        </Window.Resources>  
        <Grid>  
            <ComboBox Width="200" Height="25" ItemsSource="{Binding Source={StaticResource CountryView}}" DisplayMemberPath="Country"  
                      SelectedItem="{Binding SelectedCountry}"/>  
        </Grid>  
    

    MainWindow.xaml.cs:

    using System.Collections.ObjectModel;  
    using System.Windows;  
    using System.Windows.Data;  
      
    namespace ComboboxBindCollectionView  
    {  
      public partial class MainWindow : Window  
      {  
        public ObservableCollection<MyCountry> CountryView { get; set; }  
        public MyCountry SelectedCountry { get; set; }  
        public MainWindow()  
        {  
          InitializeComponent();  
      
          CountryView = new ObservableCollection<MyCountry>();  
          CountryView.Add(new MyCountry() { Country = "Beijing" });  
          CountryView.Add(new MyCountry() { Country = "NewYork" });  
          CountryView.Add(new MyCountry() { Country = "Shanghai" });  
          CountryView.Add(new MyCountry() { Country = "Paris" });  
          CollectionViewSource cvs = (FindResource("CountryView") as CollectionViewSource);  
          cvs.Source = CountryView;  
          DataContext = this;  
        }  
      }  
       
      public class MyCountry  
      {  
        public string Country { get; set; }  
      }  
    }  
    

    Case 3: If your CountryView is a ICollectionView, the Combobox below has no items selected by default.

    <Grid>  
            <ComboBox Width="200" Height="25" ItemsSource="{Binding ConutryView}" DisplayMemberPath="Country"  
                       SelectedItem="{Binding SelectedCountry}"  IsSynchronizedWithCurrentItem = "True"/>  
        </Grid>  
    

    MainWindow.xaml.cs:

    using System.Collections.ObjectModel;  
    using System.ComponentModel;  
    using System.Windows;  
    using System.Windows.Data;  
      
    namespace ComboboxNoSelectedItemDefault  
    {  
      public partial class MainWindow : Window  
      {  
        public MainWindow()  
        {  
          InitializeComponent();  
          DataContext = new ViewModel();  
        }  
      }  
      public class ViewModel  
      {  
        public ObservableCollection<MyCountry> Countries { get; set; }  
        public MyCountry SelectedCountry { get; set; }  
        private ICollectionView conutryView;  
      
        public ICollectionView ConutryView  
        {  
          get { return conutryView; }  
        }  
      
        public ViewModel()  
        {  
          ObservableCollection<MyCountry> countries =new ObservableCollection<MyCountry>();  
          Countries = new ObservableCollection<MyCountry>();  
          Countries.Add(new MyCountry() { Country = "Beijing" });  
          Countries.Add(new MyCountry() { Country = "NewYork" });  
          Countries.Add(new MyCountry() { Country = "Shanghai" });  
          Countries.Add(new MyCountry() { Country = "Paris" });  
          conutryView = CollectionViewSource.GetDefaultView(Countries);  
        }  
      }  
      public class MyCountry  
      {  
        public string Country { get; set; }  
      }  
    }  
    

    The result:
    196457-image.png
    196458-image.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. Viorel 112.1K Reputation points
    2022-04-23T10:53:43.193+00:00

    Try setting the initial value of SelectedCountry to null, or use this binding:

    SelectedItem="{Binding SelectedCountry, Mode=OneWayToSource}"