WPF: How to data bind a ComboBox to ListView selection and set the ComboBox value range?

Multi tool use
WPF: How to data bind a ComboBox to ListView selection and set the ComboBox value range?
I'm new in WPF land and have the following question regarding data binding.
My test app contains a ListView with cars (Colums: Type, Speed and Color). Below the list view there are some controls to control the selected car's values. Among others there's a ComboBox to choose the selected car's color.
My test app looks like this
In XAML the ListView is initialized like this:
<ListView Grid.Row="1" Name="listView" ItemsSource="{Binding Model.Cars}" SelectedValue="{Binding Model.SelectedCar}">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="Type" Width="Auto" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Speed" Width="Auto" DisplayMemberBinding="{Binding Speed}" />
<GridViewColumn Header="Color" Width="Auto"
DisplayMemberBinding="{Binding Color.Value}"/>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
The ItemsSource is Model.Cars which is an ObservableCollection of Car objects.
Car.Color is a KeyValuePair (Color, string) which is initialized by the AvailableColors of the class CarColors:
public static class CarColors
{
static Random rnd = new Random();
private static Dictionary<Color, string> availableColors = new Dictionary<Color, string>
{
{ Colors.Red, "red" },
{ Colors.Green, "green" },
{ Colors.Blue, "blue" },
{ Colors.Yellow, "yellow" },
{ Colors.Brown, "brown" },
{ Colors.Silver, "silver" },
};
public static Dictionary<Color, string> GetAvailableColors()
{
return availableColors;
}
public static KeyValuePair<Color, string> GetRandomColor()
{
return availableColors.ElementAt(rnd.Next(0, availableColors.Count));
}
}
I want to data bind the Color ComboBox with the color of the selected car in the ListView. My current XAML code doesn't work:
<ComboBox Grid.Row="1" Grid.Column="1" Margin="2"
ItemsSource="{Binding Source={StaticResource AvailableColors}}"
SelectedValuePath="Key"
DisplayMemberPath="Value"
SelectedValue="{Binding ElementName=listView, Path=SelectedValue}"/>
How can I data bind the Color ComboBox so that it represents the color of the selected car but taking its value range from the static CarColors dictionary?
1 Answer
1
If your car color property is KeyValuePair try this:
<ComboBox Grid.Row="1" Grid.Column="1" Margin="2"
ItemsSource="{Binding AvailableColors}"
SelectedItem="{Binding ElementName=listView, Path=SelectedItem.Color}"
DisplayMemberPath="Value"/>
NB. the color property of any car object must point to an element of AvailableColors source color list.
ie.
new Car { Name = "Ford", Speed = 180f, Color = AvailableColors.ElementAt(1)},
Thank you! This did the trick, now it's working:-)
– phibel
Jul 3 at 7:04
You are wellcome! :)
– Gianluca Conte
Jul 3 at 7:41
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You should bind the selectedvalue of the combo to 'SelectedValue.Color' because ListView SelectedValue is a Car, not a color.
– Babbillumpa
Jul 2 at 10:12