Initialization of ComboBox in datagrid, Silverlight 4.0

Posted by Budda on Stack Overflow See other posts from Stack Overflow or by Budda
Published on 2010-05-30T03:33:46Z Indexed on 2010/05/30 3:42 UTC
Read the original article Hit count: 350

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.

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about mvvm