组合框wpf绑定列表更改

本文关键字:列表 绑定 wpf 组合 | 更新日期: 2023-09-27 18:04:19

我有一个关于在重新加载列表中绑定所选项目的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);
        }
    }
}
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正在为组合框绑定创建列表,setButton将id设置为3。然后如果我再次填充列表,在组合框中选择Id为1。不是应该还是3吗?我的意思是,组合框不应该为id=3的模型设置Selected Item吗?

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

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

组合框wpf绑定列表更改

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

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

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