0
我對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,然後填充,我會得到預期的結果(選擇的項目是第三項)(僅限第一次使用)?
+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