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 force开发者_运维问答d 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.
DisplayMemberPath on the combobox lets you specify the path to the Property you want to display from the underlying class
精彩评论