Hi,
I am using ComboBox with bind the Collection with the SelectedItem and using another collection as ItemSource.
When I set the default value for Collection,it is not updated in ComboBox.
Code snippet:
Xaml:
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<Window.Resources>
<local:ShipCityDetailsRepository x:Key="ShipCityName"/>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="150"/>
</Grid.ColumnDefinitions>
<ComboBox Height="30" Width="100"
SelectedItem="{Binding SelectedShipCity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{StaticResource ShipCityName}" />
</Grid>
Model contains two cs files:
OrderInfo.cs:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SfDataGrid_ComboBoxDemo
{
public class OrderInfo : INotifyPropertyChanged
{
#region Private members
private int productId;
private string _productName;
private double _NoOfOrders;
private DateTime _orderDate;
private string countryName;
private ObservableCollection<string> shipCity;// = new ObservableCollection<string>();
private string _shipCity;
#endregion
#region Public properties
public int ProductId
{
get
{
return productId;
}
set
{
productId = value;
this.RaisePropertyChanged("ProductId");
}
}
public string ProductName
{
get
{
return _productName;
}
set
{
_productName = value;
RaisePropertyChanged("ProductName");
}
}
public double NoOfOrders
{
get
{
return _NoOfOrders;
}
set
{
_NoOfOrders = value;
RaisePropertyChanged("NoOfOrders");
}
}
public DateTime OrderDate
{
get
{
return _orderDate;
}
set
{
_orderDate = value;
RaisePropertyChanged("OrderDate");
}
}
public string CountryName
{
get
{
return countryName;
}
set
{
countryName = value;
RaisePropertyChanged("CountryDescription");
}
}
public string ShipCity
{
get
{
_shipCity = string.Empty;
if (SelectedShipCity == null)
return _shipCity;
foreach (var city in SelectedShipCity)
{
if (string.IsNullOrEmpty(_shipCity))
{
_shipCity = city;
continue;
}
_shipCity += ", " + city;
}
return _shipCity;
}
set
{
_shipCity = value;
RaisePropertyChanged("ShipCity");
}
}
public ObservableCollection<string> SelectedShipCity
{
get
{
return shipCity;
}
set
{
shipCity = value;
RaisePropertyChanged("SelectedShipCity");
}
}
#endregion
public OrderInfo()
{
this.SelectedShipCity = new ObservableCollection<string>();
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}
ShipCityDetailsRepository.cs:
public class ShipCityDetailsRepository : ObservableCollection<string>
{
public ShipCityDetailsRepository()
{
this.Add("Buenos Aires");
this.Add("Graz");
this.Add("Bruxelles");
this.Add("Campinas");
this.Add("Montréal");
this.Add("Århus");
this.Add("Helsinki");
this.Add("Lille");
this.Add("Aachen");
this.Add("Bergamo");
this.Add("Cork");
this.Add("México D.F.");
}
}
public class DisabledShipCityDetailsRepository : ObservableCollection<string>
{
public DisabledShipCityDetailsRepository()
{
this.Add("Buenos Aires");
this.Add("Århus");
this.Add("Helsinki");
this.Add("Lille");
this.Add("México D.F.");
}
}
In ViewModel.cs :
public class ViewModel : INotifyPropertyChanged
{
private ObservableCollection<string> shipCity;
public ObservableCollection<string> SelectedShipCities
{
get
{
return shipCity;
}
set
{
shipCity = value;
RaisePropertyChanged("SelectedShipCities");
}
}
private ObservableCollection<OrderInfo> _orderList;
public ObservableCollection<OrderInfo> OrderList
{
get
{
return _orderList;
}
set
{
_orderList = value;
RaisePropertyChanged("OrderList");
}
}
public ViewModel()
{
SelectedShipCities = new ObservableCollection<string> { "Buenos Aires" };
_orderList = new ObservableCollection<OrderInfo>();
_orderList.Add(new OrderInfo()
{
ProductId = 1001,
ProductName = "Alice Mutton",
NoOfOrders = 10,
OrderDate = new DateTime(2015, 5, 2),
CountryName = "Argentina",
SelectedShipCity = new ObservableCollection<string>() { "Bruxelles" }
});
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}
I need to show the SelectedShipCity = new ObservableCollection<string>() { "Bruxelles" } this value in viewmodel.cs in my combobox.
Please do the favor to get the solution.