当用户取消选择时继续突出显示单元格

Keep highlighting the cell when user deselect

我正在使用 Xamarin Forms ListView 作为边栏。如何防止用户取消选择单元格?或者至少在用户取消选择时继续突出显示该单元格。 这就是我绑定的方式


                   <ListView x:Name="listView" SelectionMode="Single">
                        <ListView.ItemsSource>
                            <x:Array Type="{x:Type x:String}">
                                <x:String>Management</x:String>
                                <x:String>Information</x:String>
                                <x:String>Language</x:String>
                                <x:String>Settings</x:String>
                            </x:Array>
                        </ListView.ItemsSource>
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <TextCell Text="{Binding}" />
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>

根据您的需要,我做了类似的事情,但每行内都有控件,例如复选框。
https://xamarinhelp.com/multiselect-listview-xamarin-forms/

使用ListView的SelectedItem 属性。只要 SelectedItem 属性 没有设置回 null,当前选中的项目将保持突出显示。

根据您的描述,当您从 ListView 中 select item 时,此 item 突出显示,您希望此 item 在未处于 selected 状态时仍然突出显示。您似乎想要 select ListView 中的多个项目。

我做了一个样例,你可以看看:

 <ContentPage.Content>
    <StackLayout>
        <ListView
            ItemTapped="ListView_ItemTapped"
            ItemsSource="{Binding Items}"
            SelectionMode="Single">

            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout BackgroundColor="{Binding background}" Orientation="Horizontal">
                            <Label
                                HorizontalOptions="StartAndExpand"
                                Text="{Binding DisplayName}"
                                TextColor="Fuchsia" />
                            <BoxView
                                HorizontalOptions="End"
                                IsVisible="{Binding Selected}"
                                Color="Fuchsia" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage.Content>

public partial class Page10 : ContentPage
{
    public Page10 ()
    {
        InitializeComponent ();
        this.BindingContext = new MultiSelectItemsViewModel();
    }

    private void ListView_ItemTapped(object sender, ItemTappedEventArgs e)
    {
        Model m = e.Item as Model;

        if(m!=null)
        {
            m.Selected = !m.Selected;
            if(m.background==Color.White)
            {
                m.background = Color.BlueViolet;
            }
            else
            {
                m.background = Color.White;
            }
        }

    }
}

public class Model:ViewModelBase
{
    public string DisplayName { get; set; }
    private bool _Selected;

    public bool Selected
    {
        get { return _Selected; }
        set
        {
            _Selected = value;
            RaisePropertyChanged("Selected");
        }
    }
    private Color _background;
    public Color background
    {
        get { return _background; }
        set
        {
            _background = value;
            RaisePropertyChanged("background");
        }
    }
}

public class MultiSelectItemsViewModel
{
    public ObservableCollection<Model> Items { get; set; }


    public MultiSelectItemsViewModel()
    {
        Items = new ObservableCollection<Model>();
        Items.Add(new Model() { DisplayName = "AAA", Selected = false,background=Color.White });
        Items.Add(new Model() { DisplayName = "BBB", Selected = false , background = Color.White });
        Items.Add(new Model() { DisplayName = "CCC", Selected = false, background = Color.White });
        Items.Add(new Model() { DisplayName = "DDD", Selected = false, background = Color.White });
        Items.Add(new Model() { DisplayName = "EEE", Selected = false, background = Color.White });

    }


}

如果我的回复对您有帮助,请记得将我的回复标记为已答复,谢谢

更新:

不允许用户取消select selected 项目。

 private void ListView_ItemTapped(object sender, ItemTappedEventArgs e)
    {
        Model m = e.Item as Model;

        if(m!=null)
        {
            m.Selected = true;
            m.background = Color.Blue;


        }

    }