phibel
phibel

Reputation: 171

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?

Upvotes: 1

Views: 819

Answers (1)

Gianluca Conte
Gianluca Conte

Reputation: 500

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)},

Upvotes: 0

Related Questions