0

我有一个关于在重新加载列表上绑定选定项目的 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);
        }
    }
}

绑定视图模型:

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

}

xml:

    <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 正在为组合框绑定创建列表,setButton 将 id 设置为 3。然后,如果我再次填充列表,则组合框中选择的 ID 为 1。它不应该还是 3 吗?我的意思是,组合框不应该为 id=3 的模型设置 Selected Item。

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

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

4

1 回答 1

4

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

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

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

于 2013-03-16T17:46:52.793 回答