question

Sarah-3412 avatar image
0 Votes"
Sarah-3412 asked HuiLiu-MSFT commented

How to set combobox by default to no selected item

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-wpf
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,@Sarah-3412. Is there any update? Did my answer solve your problem? If so, you could accept it as the answer. It's helpful for community members with similar questions.

0 Votes 0 ·
Viorel-1 avatar image
0 Votes"
Viorel-1 answered HuiLiu-MSFT commented

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

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


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @Viorel-1 , I tried both of your suggestions. Unfortunately none of them worked.

0 Votes 0 ·

Maybe there are some specific details. In other circumstances the suggestions seem to work.


0 Votes 0 ·

Hi @Viorel-1 , For the ComboBox I use the following view. Can it be due to?

 private CollectionViewSource CvsCountrys = new CollectionViewSource();
 public ICollectionView CountryView
 {
     get
     {
         if (CvsCountrys.Source == null) CvsCountrys.Source = SqliteDataAccess.Instance.GetCountrys();
         CvsCountrys.View.Filter = (d) => ((Data)d).FK == 0;
         return CvsCountrys.View;
     }
 }


0 Votes 0 ·
HuiLiu-MSFT avatar image
0 Votes"
HuiLiu-MSFT answered HuiLiu-MSFT commented

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.


image.png (861 B)
image.png (3.0 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @HuiLiu-MSFT , Thanks for your reply. And if my CountryView is ICollectionView?

 private CollectionViewSource CvsCountrys = new CollectionViewSource();
 public ICollectionView CountryView
 {
     get
     {
         if (CvsCountrys.Source == null) CvsCountrys.Source = SqliteDataAccess.Instance.GetMainEnum();
         CvsCountrys.View.Filter = (d) => ((Data)d).FK == 0;
         return CvsCountrys.View;
     }
 }
0 Votes 0 ·

Hi,@Sarah-3412 .I updated my answer, you could try to refer to it.

0 Votes 0 ·

Hi,@Sarah-3412 . Did my answer solve your problem? If so, you could accept it as the answer. It's helpful for community members with similar questions.

0 Votes 0 ·