views:

662

answers:

1

I have datagrid with list of MyPlayer objects linked to ItemsSource, there are ComboBoxes inside of grid that are linked to a list of inner object, and binding works correctly: when I select one of the item then its value is pushed to data model and appropriately updated in other places, where it is used.

The only problem: initial selections are not displayed in my ComboBoxes. I don't know why..?

Instance of the ViewModel is assigned to view DataContext. Here is grid with ComboBoxes (grid is binded to the SquadPlayers property of ViewModel):

    <data:DataGrid ="True" AutoGenerateColumns="False" ItemsSource="{Binding SquadPlayers}">
        <data:DataGrid.Columns>
            <data:DataGridTemplateColumn Header="Rig." Width="50">
                <data:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox SelectedItem="{Binding Rigid, Mode=TwoWay}"
                                  ItemsSource="{Binding IntLevels, Mode=TwoWay}"/>
                    </DataTemplate>
                </data:DataGridTemplateColumn.CellTemplate>
            </data:DataGridTemplateColumn>
        </data:DataGrid.Columns>
    </data:DataGrid>

Here is ViewModel class ('_model_DataReceivedEvent' method is called asynchronously, when data are received from server):

public class SquadViewModel : ViewModelBase<SquadModel>
{
    public SquadViewModel()
    {
        SquadPlayers = new ObservableCollection<SquadPlayer>();
    }

    private void _model_DataReceivedEvent(List<SostavPlayerData> allReadyPlayers)
    {
        TeamTask task = new TeamTask { Rigid = 1 };
        foreach (SostavPlayerData spd in allReadyPlayers)
        {
            SquadPlayer sp = new SquadPlayer(spd, task);
            SquadPlayers.Add(sp);
        }
        RaisePropertyChanged("SquadPlayers");
    }

And here is SquadPlayer class (it's objects are binded to the grid rows):

public class SquadPlayer : INotifyPropertyChanged
{
    public SquadPlayer(SostavPlayerData spd)
    {
        _spd = spd;

        Rigid = 2;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private int _rigid;
    public int Rigid
    {
        get { return _rigid; }
        set
        {
            _rigid = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Rigid"));
            }
        }
    }

    private readonly ObservableCollection<int> _statIntLevels = new ObservableCollection<int> { 1, 2, 3, 4, 5 };
    public ObservableCollection<int> IntLevels { get { return _statIntLevels; } }

It is expected to have all "Rigid" comboboxes set to "2" value, but they are not selected (items are in the drop-down list, and if any value is selected it is going to ViewModel).

What is wrong with this example? Any help will be welcome.

Thanks.

+1  A: 

Change the order of your XAML to

ItemsSource="{Binding IntLevels, Mode=TwoWay}" 
SelectedItem="{Binding Rigid, Mode=TwoWay}"

I know it seems funny but I have had this problem before. It is attempting to bind the SelectedValue before the ItemSource exists, which is why after the initialization everything works.

Agies
I am really surprised! It helped! Thanks a lot!
Budda