0
votes

I have the following XAML:

<ListBox SelectedItem="{Binding SelectedTeam}">
    <ListBoxItem Content="{Binding Match.HomeTeam}" />
    <ListBoxItem Content="{Binding Match.RoadTeam}" />
</ListBox>

The two teams of Match are present in the listbox. But when I click on one of the items, to set the SelectedTeam property of the view-model, I have this message in the output window of Visual Studio:

System.Windows.Data Error: 23 : Cannot convert 'System.Windows.Controls.ListBoxItem: Emidee.CommonEntities.Team' from type 'ListBoxItem' to type 'Emidee.CommonEntities.Team' for 'en-US' culture with default conversions; consider using Converter property of Binding. NotSupportedException:'System.NotSupportedException: TypeConverter cannot convert from System.Windows.Controls.ListBoxItem.

One way to solve this would be to create an IEnumerable on my view-model, which would return Match.HomeTeam and Match.RoadTeam, and bind this property to the ItemsSource property of the listbox.

But is there another solution, which would allow me to specify the items in XAML like I did?

Thanks in advance

Mike

1

1 Answers

3
votes

Use SelectedValue with combination with SelectedValuePath:

<ListBox SelectedValue="{Binding SelectedTeam}"
         SelectedValuePath="Content">
    <ListBoxItem Content="{Binding Match.HomeTeam}" />
    <ListBoxItem Content="{Binding Match.RoadTeam}" />
</ListBox>