使用MVVM模式在Silverlight组合框中选择第一项

时间:2011-02-09 10:10:30

标签: silverlight mvvm silverlight-4.0 combobox

这是我第一次涉足MVVM,现在我遇到了以下问题:

我有我的ViewModel:

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

            SelectedWorkCellGroup = _workCellGroupInfoCollection.FirstOrDefault();
        }
    }

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

和我的XAML:

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

在第一次加载时,组合框会填充数据但我无法选择第一个项目。我做错了什么?

WorkCellGroupInfo派生自FilterBase类:

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

    public string Name
    {
        get;
        set;
    }
}

1 个答案:

答案 0 :(得分:2)

您需要为SelectedWorkCellGroup属性分配值才能执行此操作。

在ViewModel的构造函数中,编写以下代码:

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

编辑:

以下为我效劳:

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>

代码背后:

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));
        }
    }
}