C# 选择时,如何在行上显示和图像?

C# 选择时,如何在行上显示和图像?,c#,xamarin.forms,portable-class-library,C#,Xamarin.forms,Portable Class Library,我是Xamarin.Forms的新手,我试图在TableView上显示和图标作为选中的指示器进入选中的项目,但实际上我有点迷路了 我的问题是,现在图标在列表的每个元素中都是可见的,我希望在默认情况下将它们设置为隐藏,并且当用户选择列表的任何元素时,显示它,当然,隐藏取消选择行的图标 有什么要完成的暗示吗?任何想法或暗示都应该非常有用 以下是列表代码: <base:ListView ItemsSource="{Binding DeliveryMetho

我是Xamarin.Forms的新手,我试图在TableView上显示和图标作为选中的指示器进入选中的项目,但实际上我有点迷路了

我的问题是,现在图标在列表的每个元素中都是可见的,我希望在默认情况下将它们设置为隐藏,并且当用户选择列表的任何元素时,显示它,当然,隐藏取消选择行的图标

有什么要完成的暗示吗?任何想法或暗示都应该非常有用

以下是列表代码:

<base:ListView
                    ItemsSource="{Binding DeliveryMethodList}"
                    HorizontalOptions="FillAndExpand"
                    VerticalOptions="FillAndExpand"
                    RowHeight="60"
                    HeightRequest="120"
                    BackgroundColor="Transparent"
                    IsPullToRefreshEnabled="false"
                    SeparatorVisibility="None"
                    SelectedItem="{Binding SelectedDeliveryMethod}">

                    <base:ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                             <StackLayout 
                                    Orientation="Horizontal"
                                    HorizontalOptions="FillAndExpand"
                                    VerticalOptions="CenterAndExpand">
                                    <base:Image
                                        HeightRequest="{base:PointSize 15}"
                                        WidthRequest="{base:PointSize 15}"
                                        Source="{x:Static res:Images.AcceptIco}"
                                        IsVisible="{Binding IsSelectedDeliveryIconVisible}"
                                        VerticalOptions="Center"
                                        HorizontalOptions="Center"/>  

                                    <base:Label 
                                        FontSize="{StaticResource FontSizeMedium1}"
                                        Text="{Binding name}"
                                        HorizontalOptions="Center"
                                        VerticalOptions="Start"
                                        TextColor="White"/>
                             </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </base:ListView.ItemTemplate>
                </base:ListView>
下面是我的ViewModel方法,用于选择和项目源:

    bool _isSelectedDeliveryIconVisible;
    public bool IsSelectedDeliveryIconVisible
    {
        get { return _isSelectedDeliveryIconVisible; }
        set { SetProperty(ref _isSelectedDeliveryIconVisible, value); }
    }

    ObservableCollection<DeliveryMethod> _deliveryMethodList;
    public ObservableCollection<DeliveryMethod> DeliveryMethodList
    {
        get { return _deliveryMethodList; }
        set { SetProperty(ref _deliveryMethodList, value); }
    }

    DeliveryMethod _selectedDeliveryMethod;
    public DeliveryMethod SelectedDeliveryMethod
    {
        get { return _selectedDeliveryMethod; }
        set
        {
            SetProperty(ref _selectedDeliveryMethod, value);
            if (_selectedDeliveryMethod != null)
            {
                IsSelectedDeliveryIconVisible = true;
            }
        }
    }

最好的

我会将IsSelectedDeliveryInvisible作为DeliveryMethod类的一个属性,这样它就可以为列表中的每个项目都有一个不同的值

当用户选择交付方法时,您需要为列表中的每个项目设置IsSelectedDeliveryInvisible,请参见下面的代码。这将确保一次最多可以看到一个图标。您现有的绑定应该可以使用此新代码

public class DeliveryMethod 
{
    public int Id {get; set;}
    public bool IsSelectedDeliveryIconVisible {get; set;}
    // ...
}

DeliveryMethod _selectedDeliveryMethod;
public DeliveryMethod SelectedDeliveryMethod
{
    get { return _selectedDeliveryMethod; }
    set
    {
        SetProperty(ref _selectedDeliveryMethod, value);
        if (_selectedDeliveryMethod != null)
        {
            DeliveryMethodList.ForEach(d => { d.IsSelectedDeliveryIconVisible = (d.Id == value.Id); });
        }
    }
}

在哪里可以选择交货?这是VM上的属性还是DeliveryMethod上的属性?请记住,列表中的每个单元格都绑定到DeliveryMethod的一个实例。IsSelectedDeliveryInvisible是VM上的一个属性。