【问题标题】:Keep highlighting the cell when user deselect用户取消选择时继续突出显示单元格
【发布时间】:2020-03-08 10:49:09
【问题描述】:

我使用 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>

【问题讨论】:

标签: xaml listview xamarin xamarin.forms highlight


【解决方案1】:

根据你的描述,当你从ListView中选择item时,这个item高亮,你想让这个item在这个item没有被选中的状态下仍然高亮。您似乎想从 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 });

    }


}

如果我的回复对你有帮助,请记得将我的回复标记为答案,谢谢。

更新:

不允许用户取消选择选定的项目。

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

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


        }

    }

【讨论】:

  • 你误会了。我想要的是总是有 1 个突出显示的项目,只有 1 个。用户无法做任何事情来取消突出显示它
  • @JonFlame,你的意思是一旦选中了就不能取消选中了?
  • 这就是我想要的。我希望用户在选择项目后不能取消选择所选项目
  • @JonFlame,如果您不允许用户在选择项目后取消选择项目,您可以看到我的更新,只需更改 ListView_ItemTapped 事件中的一些代码。
  • 成功了,谢谢。但是我们可以在不绑定任何东西的情况下做到这一点吗?
【解决方案2】:

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

【讨论】:

  • 我试过了。它没有用。它保留了SelectedItem,但没有突出显示
【解决方案3】:

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

【讨论】:

  • 我只想使用ListView 中可用的东西。我不想在里面绑定任何东西
猜你喜欢
  • 2018-03-12
  • 2015-11-24
  • 2013-11-07
  • 1970-01-01
  • 2018-04-27
  • 2011-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多