因此,我有一个基本的视图模型,其功能如下:
// The ICollectionVIew is what my ListBox binds to.
public ICollectionView UserView { get; set; }
// <signup> is a model that's populated from a database representing a signup table
private ObservableCollection<signup> _signup;
public ObservableCollection<signup> Signup
{
get
{
return _signup;
}
set
{
if (_signup != value)
{
value = _signup;
}
OnPropertyChanged("Signup");
}
}
// This is the constructor for the ViewModel
public registrationVM()
{
// entity context Fills up the Model
context.signups.Load();
// The below code fills up the ObservableCollection
var query = context.signups;
_signup = new ObservableCollection<signup>(query);
// And the below code fills up the ICollectionView using the ObservableCollection
UserView = CollectionViewSource.GetDefaultView(_signup);
}所以现在,我可以绑定到ObservableCollection,而不是绑定到ICollection。
<ListBox ItemsSource="{Binding UserView}" DisplayMemberPath="firstName" SelectedItem="{Binding SelectedUser}"/>在加载我的信息方面,这是非常有效的。但现在出现了导航问题。我将按钮命令绑定到ViewModel,
<Button x:Name="next" Command="{Binding Next}"/>在它的执行方法中:
private object Next_CommandExecute(object param)
{
// 'UserView' Is the ICollectionView I declared earlier
return UserView.MoveCurrentToNext();
}问题是按钮的功能不起任何作用。“前一个”按钮也是如此。屏幕上选择的记录不会改变,所以我猜我做错了什么。我到底想不出的是什么。有人看到我哪里出错了吗?
发布于 2014-11-20 11:02:15
正如my comment中提到的,您需要在您的ListBox上设置IsSynchronizedWithCurrentItem = true
ListBox ItemsSource="{Binding UserView}"
DisplayMemberPath="firstName"
IsSynchronizedWithCurrentItem = true
SelectedItem="{Binding SelectedUser}"/>https://stackoverflow.com/questions/27035241
复制相似问题