views:

78

answers:

1

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.

+3  A: 

The problem is that PlayersTest is a field, not a property. Binding only works with properties. You can declare it using the auto property syntax to keep the code in your view model similar:

public ObservableCollection<SostavPlayerData> PlayersTest { get; private set; }

Then you just need to set it to a new instance of ObservableCollection<SostavPlayerData>, and you'll be all set.

Andy
Stupid error... :( Thank you for help, that works now :)
Budda