Try setting the initial value of SelectedCountry to null, or use this binding:
SelectedItem="{Binding SelectedCountry, Mode=OneWayToSource}"
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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}"/>
Try setting the initial value of SelectedCountry to null, or use this binding:
SelectedItem="{Binding SelectedCountry, Mode=OneWayToSource}"
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:
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.