xamarin - 无法使用命令 NO EVENT 在 CollectionView 中选择相同的项目两次
问题描述
我无法选择一个项目两次。我不使用事件。我用命令和 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
给您的视图模型,然后您可以使用清理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>();
}
推荐阅读
- android - Android 试图从公钥字节数组中获取公钥:java.lang.IllegalArgumentException: Invalid point encoding 0x30
- java - 引起:org.springframework.beans.FatalBeanException:检测到依赖循环
- git - 用分支上的第一个提交压缩合并提交
- wordpress - Wordpress:负载均衡器后面的一个写入器节点和几个读取器节点。无法将登录定向到仅写入器节点
- swiftui - SwiftUI 中绑定的无关参数
- visual-studio-code - VSCode / csproj / tasks.json - 添加自定义构建步骤
- php - 从 Google Cloud Logging 读取日志时出现 DEADLINE_EXCEEDED
- reactjs - 声明文件和 React 组件道具是否推荐使用 TypeScript 接口?
- java - 如何立即制作 Facebook Audience 插页式广告?
- css - 下拉菜单选项位于我的 React 模式页面的后面或下方