所以我有一个基本的视图模型,其功能如下:
// 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();
}
问题是按钮的功能没有做任何事情。 “前一个”按钮也是如此。屏幕上选择的记录没有改变所以我猜我有些错误。究竟是什么我一直未能弄明白。有谁看到我哪里出错?
答案 0 :(得分:1)
正如my comment中所述,您需要在IsSynchronizedWithCurrentItem = true
上设置ListBox
ListBox ItemsSource="{Binding UserView}"
DisplayMemberPath="firstName"
IsSynchronizedWithCurrentItem = true
SelectedItem="{Binding SelectedUser}"/>