C# 如何在ICollectionView中导航?
我有一个基本的视图模型,它的功能如下:C# 如何在ICollectionView中导航?,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,我有一个基本的视图模型,它的功能如下: // 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>
// 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);
}
问题是按钮的功能没有任何作用。“上一步”按钮也是如此。屏幕上选择的记录没有改变,所以我猜我做错了什么。到底什么是我一直没有弄清楚的。有人知道我哪里出错了吗?如中所述,您需要在列表框中设置IsSynchronizedWithCurrentItem=true
ListBox ItemsSource="{Binding UserView}"
DisplayMemberPath="firstName"
IsSynchronizedWithCurrentItem = true
SelectedItem="{Binding SelectedUser}"/>
是否确定已调用Next_CommandExecute
函数?(例如通过断点)@Fratyx我很确定它被调用了。我把一个消息框放在它的位置进行检查,点击按钮它就会弹出。即使我将命令放入构造函数中,也不会发生任何事情。。。老实说,我不知道断点是什么…试着在你的列表框上设置IsSynchronizedWithCurrentItem=true
。
@michaelmoore请把它作为一个答案,这样我可以标记它。非常感谢。@报价完成,谢谢。
<Button x:Name="next" Command="{Binding Next}"/>
private object Next_CommandExecute(object param)
{
// 'UserView' Is the ICollectionView I declared earlier
return UserView.MoveCurrentToNext();
}
ListBox ItemsSource="{Binding UserView}"
DisplayMemberPath="firstName"
IsSynchronizedWithCurrentItem = true
SelectedItem="{Binding SelectedUser}"/>