问题描述
我无法选择一个项目两次。我不使用事件。我用命令和 viewmodel 管理一切。 我只能找到 CodeBehind 的解决方案。
代码隐藏解决方案 ((CollectionView)sender).SelectedItem = null;
我不使用发件人,因此此变体不起作用。
这是我的命令调用:
<CollectionView
SelectionMode="Single"
ItemsSource="{Binding Source={RelativeSource AncestorType={x:Type Lib:Profile_Model}},Path=UserPartyById}"
ItemTemplate="{StaticResource Profile_Party_DataTemplate}"
SelectedItem="{Binding Source={RelativeSource AncestorType={x:Type Lib:Profile_Model}},Path=SelectedItem}"
SelectionChangedCommand="{Binding Source={RelativeSource AncestorType={x:Type Lib:Profile_Model}},Path=GoPartyDetailCommand}"
>
<CollectionView.ItemsLayout>
<LinearItemsLayout ItemSpacing="5" Orientation="Horizontal" />
</CollectionView.ItemsLayout>
</CollectionView>
GoPartyDetailCommand = new Command(GoPartyDetail);
private Merged_Model selectedItem;
public Merged_Model SelectedItem
{
get => selectedItem;
set => SetPorperty(ref selectedItem,value);
}
public async void GoPartyDetail()
{
Preferences.Set("SelectPartyId",SelectedItem.Id);
Preferences.Set("FK_User",SelectedItem.FK_User);
await _navigationService.NavigatetoAsync<PartyDetail_viewmodel>();
}
H
解决方法
您可以使用 SelectionChangedCommandParameter
将您的 ColletionView
传递给您的视图模型,然后您可以使用 clean SelectedItem
。
<CollectionView
x:Name = "collectionview"
SelectionMode="Single"
ItemsSource="{Binding Source={RelativeSource AncestorType={x:Type Lib:Profile_Model}},Path=UserPartyById}"
ItemTemplate="{StaticResource Profile_Party_DataTemplate}"
SelectedItem="{Binding Source={RelativeSource AncestorType={x:Type Lib:Profile_Model}},Path=SelectedItem}"
SelectionChangedCommand="{Binding Source={RelativeSource AncestorType={x:Type Lib:Profile_Model}},Path=GoPartyDetailCommand}"
SelectionChangedCommandParameter="{x:Reference collectionview}"
>
<CollectionView.ItemsLayout>
<LinearItemsLayout ItemSpacing="5" Orientation="Horizontal" />
</CollectionView.ItemsLayout>
</CollectionView>
然后在您的视图模型中:
public ICommand GoPartyDetailCommand => new Command<CollectionView>(GoPartyDetail);
public async void GoPartyDetail(CollectionView collectionview)
{
// you could get the collectionview,then clean the selectItem
Preferences.Set("SelectPartyId",SelectedItem.Id);
Preferences.Set("FK_User",SelectedItem.FK_User);
await _navigationService.NavigateToAsync<PartyDetail_ViewModel>();
}