Xaml 当用户取消选择时,保持高亮显示单元格

Xaml 当用户取消选择时,保持高亮显示单元格,xaml,listview,xamarin,xamarin.forms,highlight,Xaml,Listview,Xamarin,Xamarin.forms,Highlight,我使用Xamarin表单ListView作为侧边栏。如何防止用户取消选择单元格?或者至少在用户取消选择单元格时保持高亮显示。 这就是我的工作方式 管理层 问询处 语言 设置 根据您的需要,我做了类似的事情,但每行都有控件,比如复选框。 使用ListView的SelectedItem属性。只要SelectedItem属性未设置回null,当前选定的项目将保持高亮显示。根据您的描述,当您从ListView中选择项目时,此项目高亮显示,您希望此项目在未选中状态时仍高亮显示。似乎要从ListView

我使用Xamarin表单
ListView
作为侧边栏。如何防止用户取消选择单元格?或者至少在用户取消选择单元格时保持高亮显示。 这就是我的工作方式



管理层
问询处
语言
设置

根据您的需要,我做了类似的事情,但每行都有控件,比如复选框。

使用ListView的
SelectedItem
属性。只要
SelectedItem
属性未设置回null,当前选定的项目将保持高亮显示。

根据您的描述,当您从ListView中选择项目时,此项目高亮显示,您希望此项目在未选中状态时仍高亮显示。似乎要从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 });

    }


}

我试过了。它不起作用。它保留了
SelectedItem
,但没有高亮显示。我只想使用
ListView
中可用的东西。我不想把任何事情都牵扯进去,你想错了。我想要的是总是有一个突出显示的项目,只有一个。用户不能做任何事情来解除照明it@JonFlame,您的意思是,一旦选择了所选项目,您就不能取消选择它?这就是我想要的。我希望用户在选择后不能取消选择所选项目it@JonFlame,如果您不允许用户在选择项目后取消选择该项目,您可以看到我的更新,只需在ListView\u ItemTapped事件中更改一些代码即可。这已成功,谢谢。但是我们可以在不绑定任何内容的情况下执行此操作吗?您还可以将CollectionView与multiselect一起使用:)
 <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 });

    }


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

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


        }

    }