ComboBox wpf绑定列表已更改

时间:2013-03-16 17:05:41

标签: c# wpf c#-4.0 binding mvvm-light

我有一个关于ComboBox关于在重新加载列表上绑定所选项目的问题。

class Model : ViewModelBase
{
    /// <summary>
    /// The <see cref="Title" /> property's name.
    /// </summary>
    public const string TitlePropertyName = "Title";

    private string _title = "";

    /// <summary>
    /// Sets and gets the Title property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>
    public string Title
    {
        get
        {
            return _title;
        }

        set
        {
            if (_title == value)
            {
                return;
            }

            RaisePropertyChanging(TitlePropertyName);
            _title = value;
            RaisePropertyChanged(TitlePropertyName);
        }
    }

    /// <summary>
    /// The <see cref="Id" /> property's name.
    /// </summary>
    public const string idPropertyName = "Id";

    private int _myId = 0;

    /// <summary>
    /// Sets and gets the id property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>
    public int Id
    {
        get
        {
            return _myId;
        }

        set
        {
            if (_myId == value)
            {
                return;
            }

            RaisePropertyChanging(idPropertyName);
            _myId = value;
            RaisePropertyChanged(idPropertyName);
        }
    }
}

Binded ViewModel:

class MainViewModel : ViewModelBase
{
    public MainViewModel()
    {

        PopulateCommand = new RelayCommand(() =>
        {
            Populate();
        });

        SetCommand = new RelayCommand(() =>
        {
            Id = 3;
        });
    }

    public void Populate()
    {
        MyList = new ObservableCollection<Model>(){
            new Model(){
                Id = 1,
                Title = "numer1"
            },
            new Model(){
                Id = 2,
                Title = "numer2"
            },
            new Model(){
                Id = 3,
                Title = "numer3"
            },
            new Model(){
                Id = 4,
                Title = "numer4"
            },
        };
    }

    public RelayCommand PopulateCommand { get; private set; }
    public RelayCommand SetCommand { get; private set; }

    /// <summary>
    /// The <see cref="MyList" /> property's name.
    /// </summary>
    public const string MyListPropertyName = "MyList";

    private ObservableCollection<Model> _myList = null;

    /// <summary>
    /// Sets and gets the MyList property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>
    public ObservableCollection<Model> MyList
    {
        get
        {
            return _myList;
        }

        set
        {
            if (_myList == value)
            {
                return;
            }

            RaisePropertyChanging(MyListPropertyName);
            _myList = value;
            RaisePropertyChanged(MyListPropertyName);
        }
    }

    /// <summary>
    /// The <see cref="Id" /> property's name.
    /// </summary>
    public const string IdPropertyName = "Id";

    private int _id = 0;

    /// <summary>
    /// Sets and gets the Id property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>
    public int Id
    {
        get
        {
            return _id;
        }

        set
        {
            if (_id == value)
            {
                return;
            }

            RaisePropertyChanging(IdPropertyName);
            _id = value;
            RaisePropertyChanged(IdPropertyName);
        }
    }

}

XAML:

    <ComboBox
            ItemsSource="{Binding MyList}"
            DisplayMemberPath="Title" SelectedValue="{Binding Id, Mode=TwoWay}" SelectedValuePath="Id" IsSynchronizedWithCurrentItem="True"/>
    <Button Content="Populate"
            Command="{Binding PopulateCommand}" />
    <Button Content="Set"
            Command="{Binding SetCommand}" />

    <TextBlock Text="{Binding Id}" Width="100"/>

因此,populateButton正在创建用于组合框绑定的List,setButton将id设置为3.然后,如果我再次填充列表,则选择组合框中的Id为1.不应该仍为3吗?我的意思是,组合框不应该为id = 3的模型设置Selected Item。

我也试过没有将IsSynchronizedWithCurrentItem设置为true。然后在重新加载列表后,在组合框中没有选择任何内容。有没有办法将selectedItem与Id同步?

似乎应首先初始化列表,然后我应该设置Id,因此组合框可以更新所选项目。但是为什么如果我首先使用setButton,然后填充,我得到预期的结果(选择的项目是第三项)(仅在首次使用时)?

1 个答案:

答案 0 :(得分:4)

您需要做的就是在重新填充列表后通知视图Id属性已更改。

PopulateCommand = new RelayCommand(
    () =>
        {
            Populate();
            RaisePropertyChanged(() => Id);
        });

您无需使用IsSynchronizedWithCurrentItem=true。这将用于(例如)保持两个列表框同步并显示相同的选定项目。