Silverlight 4 Combobox with selectedValue使用MVVM-Light [英] Silverlight 4 Combobox with selectedValue using MVVM-Light

查看:235
本文介绍了Silverlight 4 Combobox with selectedValue使用MVVM-Light的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用MVVM-Light工具包,我遇到了以下问题:我有一个基本的Silverlight Combobox,它绑定到一个ViewModel类型为MyUser的ObservableCollection。 Combobox实现自定义DataTemplate以组合用户的名称和姓氏。加载用户列表后,如何设置Combobox以选择列表中的第一个用户,并在折叠的Combobox中显示此所选用户?我知道,它已被建议使用SelectedValue属性,但我不能得到它的工作使用SelectedItem或SelectedValue。换句话说,即使我在用户列表被加载之后设置SelectedValue / SelectedItem,所选择的MyUser不会在组合框中显示为选择,我如何实现呢?请参阅下面的XAML:

I have recently started using the MVVM-Light toolkit and I am stuck on the following problem: I have a basic Silverlight Combobox that is bound to a viewmodel with an ObservableCollection of type MyUser. The Combobox implements a custom DataTemplate to combine the user’s name and surname. After loading the list of users, how do I set the Combobox to select the first user in the list and display this selected user in the collapsed Combobox? I know that it has been suggested to use the SelectedValue property but I have not been able to get it to work using either SelectedItem or SelectedValue. Put another way, even though I set the SelectedValue/SelectedItem after the list of users has been loaded the selected MyUser does not display as selected in the combobox, how do I achieve this? Please see the XAML below:

<ComboBox
    ItemsSource="{Binding MyUsers, Mode=OneWay}"
    SelectedItem="{Binding SelectedUser, Mode=TwoWay}"
    IsEnabled="{Binding IsReady}">
       <ComboBox.ItemTemplate>
           <DataTemplate>
               <StackPanel Orientation="Horizontal">
                  <TextBlock Text="{Binding Name}"></TextBlock>
                  <TextBlock Text=" "></TextBlock>
                  <TextBlock Text="{Binding Surname}"></TextBlock>
               </StackPanel>
           </DataTemplate>
       </ComboBox.ItemTemplate>
 </ComboBox>

视图模型代码如下:

public ObservableCollection<MyUser> MyUsers
{
    get
    {
        return myUsers;
    }
    set
    {
        if (myUsers == value)
        {
            return;
        }
        myUsers = value;
        SelectedUser = myUsers.FirstOrDefault();
        IsReady = true;
        RaisePropertyChanged("MyUsers");
    }
}

public MyUser SelectedUser
{
    get
    {
        return selectedUser;
    }
    set
    {
        if (selectedUser == value)
        {
            return;
        }
        selectedUser = value;
        RaisePropertyChanged("SelectedUser");
    }
}


推荐答案

回答很简单,您必须在viewModel中设置Selected User之前为MyUsers列提出PropertyChanged事件,即在更新SelectedValue之前通知UI已经更改ItemsSource。

The answer is simple, you have to raise the PropertyChanged event for the List of MyUsers before setting the Selected User in the viewModel, i.e. notify the UI that the ItemsSource has changed before updating the SelectedValue.

这篇关于Silverlight 4 Combobox with selectedValue使用MVVM-Light的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆