WPF ComboBox binding
- by Budda
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.