1

I have a dictionary:

private Dictionary<int, ICar> _ICarsDic;

The object ICar actually contains another list of objects:

public interface ICar 
{
    int carId { get; set; }
    string carName { get; set; }
    Dictionnary<int,IBrandsDetails> brandsDetails { get; set; }
}

I am binding this CarsDic dictionnary to a DataGrid (transforming it to an IEnumerable before but that's not the point of the question so no showing here).

<DataGrid Name="Cars"
    ItemsSource="{Binding}" 
    SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}, AncestorLevel=2}, Path=SelectedCar, Mode=TwoWay}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Car Id" Binding="{Binding CarId}" IsReadOnly="True" />
        <DataGridTextColumn Header="Car Name" Binding="{Binding carName}" IsReadOnly="True" />
    </DataGrid.Columns>

My problem is that I'd like to display some data from the BrandsDetails as well (common to all the cars), like for example the logo. This synthax doesn't work though:

<DataGridTextColumn Header="Full Name" Binding="{Binding BrandsDetails.Logo}" IsReadOnly="True" />

Thank you in advance for your answers!

goul
  • 813
  • 1
  • 13
  • 32

2 Answers2

1

i htink the following link will solve your problem

http://www.dev102.com/2008/03/07/binding-a-wpf-control-to-a-dictionary/

quoting

Binding to a dictionary can be tricky.

It sounds simple but it never works on the first try. Usually when you first run you application you see that instead of the beautiful template you created for the items, you get something that looks like a pair of key and value. your binding works fine, you just did not think about what are you binding to. every item in a dictionary is a pair (Key,Value) and that is exactly what you get as the binded item. You have two ways of handle this, You can either change the markup of the Binding expression to include the reference to the Value :

<ComboBox.ItemTemplate>
             <DataTemplate>
                 <StackPanel Orientation="Horizontal">
                     <TextBlock Text="{Binding Value.Text}"></TextBlock>
                     <TextBlock> = </TextBlock>
                     <TextBlock Text="{Binding Value.Value}"></TextBlock>
                 </StackPanel>
             </DataTemplate>
         </ComboBox.ItemTemplate>

or you can change the binding expression of the control to refer to the Values property of the dictionary:

 <ComboBox Height="23" Margin="0,14,9,0" Name="comboBox1"
                  VerticalAlignment="Top" SelectedValuePath="Key"
                  ItemsSource="{Binding Items.Values}"
                  HorizontalAlignment="Right" Width="120">

Now you have your control binded to a Dictionary, Hurray!

Nahum
  • 6,959
  • 12
  • 48
  • 69
  • Hi, thank you for your answer, but I think my question wasn't clear enough as the dictionnary isn't the issues since I am "transforming it to an IEnumerable before" (cf. question). So the issue is much more that I have the Brand object inside the ICars that I seem to not be able to access its properties. – goul Apr 10 '13 at 01:42
  • I have opened another question (that I hope is clearer) as I don't know how to change this one without impacting your answer. Thanks! http://stackoverflow.com/questions/15915805/bind-to-some-embeded-object-properties – goul Apr 10 '13 at 02:21
0

For this answer I will assume that your DataGrid is bound to the Dictionary containing the ICar implementing objects (your exact binding is both unclear and overly complicated).

The reason why your BrandsDetails binding fails is that property is actually a Dictionary, and you know that doesn't have a Logo property. Ideally that DataGrid column should have a data template that is some sort of repeater control like a ListView or another DataGrid, or use a MultiBinding and a converter - that is the only way you will be able to show the complete Dictionary from that property. Otherwise you will need to use indexing in the path for your binding to pick out a single item.

Community
  • 1
  • 1
slugster
  • 49,403
  • 14
  • 95
  • 145
  • Hi, thank you for your answer, but I think my question wasn't clear enough as the dictionnary isn't the issues since I am "transforming it to an IEnumerable before" (cf. question). So the issue is much more that I have the Brand object inside the ICars that I seem to not be able to access its properties. – goul Apr 10 '13 at 01:42
  • I have opened another question (that I hope is clearer) as I don't know how to change this one without impacting your answer. Thanks! http://stackoverflow.com/questions/15915805/bind-to-some-embeded-object-properties – goul Apr 10 '13 at 02:21