开发者

Select first item in Silverlight combobox using MVVM pattern

This is my first foray in MVVM and I've now encountered the following issue:

I've got my ViewModel:

  public List<WorkCellGroupInfo> WorkCellGroupInfoCollection
    {
        get
        {
            return _workCellGroupInfoCollection;
        }
        set
        {
            _workCellGroupInfoCollection = value;
            NotifyPropertyChanged( "WorkCellGroupInfoCollection" );

            SelectedWorkCellGroup = _workCellGroupInfoCollection.FirstOrDefault();
        }
    }

    public WorkCellGroupInfo SelectedWorkCellGroup
    {
        get
        {
            return _selectedWor开发者_Go百科kCellGroup;
        }
        set
        {
            _selectedWorkCellGroup = value;
            NotifyPropertyChanged( "SelectedWorkCellGroup" );
        }
    }

and my XAML:

<ComboBox x:Name="WorkCellGroup" 
ItemsSource="{Binding WorkCellGroupInfoCollection}" 
SelectedItem="{Binding SelectedWorkCellGroup, Mode=TwoWay}" 
DisplayMemberPath="Name">

On the first load the combobox gets populated with the data but I can't get the first item selected. What am I doing wrong?

The WorkCellGroupInfo is derived from the FilterBase class:

public abstract class FilterBase
{
    public string Id
    {
        get;
        set;
    }

    public string Name
    {
        get;
        set;
    }
}


You need to assign a value to SelectedWorkCellGroup property to do that.

In you ViewModel's constructor, write following code:

if(WorkCellGroupInfoCollection.Any()) 
{
    SelectedWorkCellGroup = WorkCellGroupInfoCollection.First();
}

Edit:

Following works for me:

XAML:

<Grid x:Name="LayoutRoot"
      Background="White">
    <Border HorizontalAlignment="Center"
            VerticalAlignment="Center">
        <ComboBox x:Name="WorkCellGroup"
                  ItemsSource="{Binding WorkCellGroupInfoCollection}"
                  SelectedItem="{Binding SelectedWorkCellGroup, Mode=TwoWay}"
                  DisplayMemberPath="Name" />
    </Border>
</Grid>

Code Behind:

public partial class ComboBoxSelectedItemTest : UserControl
{
    public ComboBoxSelectedItemTest()
    {
        InitializeComponent();

        DataContext = new ComboBoxSelectedItemTestViewModel();
    }
}

public abstract class FilterBase
{
    public string Id
    {
        get;
        set;
    }

    public string Name
    {
        get;
        set;
    }
}

public class WorkCellGroupInfo : FilterBase
{ }

public class WorkCellGroupInfoCollection : ObservableCollection<WorkCellGroupInfo>
{ }

public class ComboBoxSelectedItemTestViewModel : INotifyPropertyChanged
{
    public WorkCellGroupInfoCollection WorkCellGroupInfoCollection { get; set; }

    public ComboBoxSelectedItemTestViewModel()
    {
        WorkCellGroupInfoCollection = new WorkCellGroupInfoCollection();

        for (int i = 0; i < 25; i++)
        {
            WorkCellGroupInfoCollection.Add(new WorkCellGroupInfo()
            {
                Id = String.Format("Id #{0}", i + 1),
                Name = String.Format("Name #{0}", i + 1)
            });
        }

        SelectedWorkCellGroup = WorkCellGroupInfoCollection.First();
    }

    private WorkCellGroupInfo _selectedWorkCellGroup;
    public WorkCellGroupInfo SelectedWorkCellGroup
    {
        get
        {
            return _selectedWorkCellGroup;
        }
        set
        {
            _selectedWorkCellGroup = value;
            RaisePropertyChanged("SelectedWorkCellGroup");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void RaisePropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler temp = PropertyChanged;
        if (temp != null)
        {
            temp(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜