Hi Guys,
Within my application, I have a ComboBox that a user could select multiple items from thatComboBox.
So far, I have been following this link and it is exactly the sort of thing that I needed.
After implementing it, I have came across a few issues. Firstly, within my ViewModel, I assign my properties to the data entities within my Entity Framework model within my constructor like so;
public EmployeeViewModel(DataObjects.Employee e) : base("") { Title = e.Title; FirstName = e.FirstName; Surname = e.Surname; }
But When I try to assign the values from the example site it produces this error;
Benefits = e.BenefitsProvided;
private ObservableCollection<string> _benefits = new ObservableCollection<string> {"Items"}; public ObservableCollection<string> Benefits { get { return _benefits; } } private string _selectedBenefit = "None"; public string SelectedBenefit { get { return _selectedBenefit; } set { _selectedBenefit = value; OnPropertyChanged("SelectedBenefit"); } } private ObservableCollection<string> _selectedBenefits; public ObservableCollection<string> SelectedBenefits { get { if (_selectedBenefits == null) { _selectedBenefits = new ObservableCollection<string> {"None"}; SelectedBenefitsText = WriteSelectedBenefitsString(_selectedBenefits); _selectedBenefits.CollectionChanged += (s, e) => { SelectedBenefitsText = WriteSelectedBenefitsString(_selectedBenefits); OnPropertyChanged("SelectedBenefits"); }; } return _selectedBenefits; } set { _selectedBenefits = value; } } public string SelectedBenefitsText { get { return _SelectedBenefitsText; } set { _SelectedBenefitsText = value; OnPropertyChanged("SelectedBenefitsText"); } } string _SelectedBenefitsText;
private static string WriteSelectedBenefitsString(IList<string> list)
{
if (list.Count == 0)
return String.Empty;
StringBuilder builder = new StringBuilder(list[0]);
for (int i = 1; i < list.Count; i++)
{
builder.Append(", ");
builder.Append(list[i]);
}
return builder.ToString();
}
Error 2 Cannot implicitly convert type 'string' to 'System.Collections.ObjectModel.ObservableCollection<string>'
This is how I bind this to my xaml;
<src:MultiComboBox Grid.Column="1" Grid.Row="9" SelectionMode="Multiple" VerticalAlignment="Center" DisplaySeparator=", " ItemsSource="{Binding Benefits}" SelectedItems="{Binding SelectedBenefits}"></src:MultiComboBox>
I understand that you can not assign a property of ObservableCollection<string>to a string. Which leads me to my question, is there some sort of converter or a way of saving a collection to an entity field within my EntityFramework model. (BenefitsProvided is a nvarchar within EF)
Many thanks,
Greg.