views:

139

answers:

1

Guys

I'm trying to bind a "second level" property of my class to a combobox.

What am i doing? I'm assigning a ObservableCollection to be the datacontext of the grid that hold all my textboxes and one combobox.

The Hierarchy can be described as

public class ListaLogradouro : ObservableCollection<Logradouro>
{

}

public class Logradouro
{
    public int CodLogradouro { get; set; }
    public string Cep { get; set; }
    public string Estado { get; set; }
    public string Cidade { get; set; }
    public ListaBairro Bairros { get; set; }
    public string Lograd { get; set; }
    public string Localizacao { get; set; }
    public string Complemento { get; set; }
    public string Numero { get; set; }

}

as you can see, the property "Bairros" is another ObservableCollection, of Bairro this time.

I was trying to bind using

<ComboBox Background="DarkGray" Height="23" HorizontalAlignment="Left" IsEditable="True" IsTextSearchEnabled="True" Margin="519,17,0,0" Name="cmbBairro" VerticalAlignment="Top" Width="202" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Path=CurrentItem.Bairros}"/>

just following this link from microsoft. Worked, but, somehow, i dont like to "force" the entire collection to be displayed by overriding ToString method. I sense i could do it better, not being forced to override ToString of every single classe i'd like to display later, in a combo, grid, etc.

I just gave a fine search within this forum but couldnt find any good clue. Is there any way i could implement it avoiding the ToString's override?

Thanks in advance and sorry for my english.

+1  A: 

DisplayMemberPath on the combobox lets you specify the path to the Property you want to display from the underlying class

IanR
Thanks Ian. I just tried your way before i implement this one, but with no success (ok, got success but i had to , again, override ToString().I dont know if it's losing itself while search for the ancestor type of that property, and after all, i just followed microsoft's example. As i said, works, but it could be written in a better and clear way. I just don't know which way it should be...
Edward