微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法使用 Command NO EVENT

如何解决无法使用 Command NO EVENT

我无法选择一个项目两次。我不使用事件。我用命令和 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>();

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。