WPF ComboBox binding

Posted by Budda on Stack Overflow See other posts from Stack Overflow or by Budda
Published on 2010-05-01T22:16:46Z Indexed on 2010/05/01 22:27 UTC
Read the original article Hit count: 382

Filed under:
|
|
|

Here is peace of the XAML code from my page:

            <ComboBox Grid.Row="2" Grid.Column="1" Name="Player2" MinWidth="50"
                ItemsSource="{Binding PlayersTest}" DisplayMemberPath="ShortName">

custom object is binded to the page data context:

        page.DataContext = new SquadViewModel();

Here is part the source code of 'SquadViewModel' class:

public class SquadViewModel
{
    public SquadViewModel()
    {
        PlayersTest = new ObservableCollection<SostavPlayerData>();
        PlayersTest.Add(new SostavPlayerData { ShortName = "A. Sereda", });
        PlayersTest.Add(new SostavPlayerData { ShortName = "D. Sereda", });
    }

    public readonly ObservableCollection<SostavPlayerData> PlayersTest;

    public string TestText { get { return "Binding works perfectly!"; } }
}

As a result ComboBox should display a list of objects, but it is empty.

Do you know why and how to get this list?

Thank you.

P.S. I've tried another XAML markup

            <ComboBox Grid.Row="1" Grid.Column="1" Name="Player1" MinWidth="50"
                ItemsSource="{Binding PlayersTest}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding ShortName}"/>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

It doesn't work also, but binding to simple text block:

        <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding TestText}"/>

Works perfectly.

© Stack Overflow or respective owner

Related posts about wpf

Related posts about combobox