ComboBox wpf绑定列表已更改
问题描述:
我对ComboBox有关于在重新加载列表上绑定选定项目的问题。ComboBox wpf绑定列表已更改
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:
所以,populateButton正在为组合框绑定列表,SET按钮设置ID为3.然后,如果我填充列表再次choosen组合框中的ID是1.是不是应该还是3?我的意思是,不应该组合框为ID = 3的模型设置Selected Item。
我也试过没有IsSynchronizedWithCurrentItem设置为true。然后重新加载列表后,组合框中没有任何选择。有没有办法使用ID同步selectedItem?
好像列表应该先初始化,然后我应该设置ID,所以组合框可以更新选定的项目。但是,那么为什么如果我先使用setButton,然后填充,我会得到预期的结果(选择的项目是第三项)(仅限第一次使用)?
答
您需要做的就是在重新填充列表后通知视图:Id属性已更改。
PopulateCommand = new RelayCommand(
() =>
{
Populate();
RaisePropertyChanged(() => Id);
});
您不需要使用IsSynchronizedWithCurrentItem=true
。这将用于(例如)保持两个列表框同步并显示相同的选定项目。
+1。取决于如何实现'RaisePropertyChanged'方法,可能需要使用一个字符串:'RaisePropertyChanged(“Id”);' – 2013-03-16 17:52:12
OP使用MVVM light的ViewModelBase(我认为)。 – Phil 2013-03-16 17:59:29
谢谢,能否通知其他对象?做类似: RaisePropertyChanged(()=> OtherObject.Id)?看起来这不起作用。 – Mat38 2013-03-16 18:20:38