简体   繁体   English

如何在 CollectionView(水平)中直观地使我的选定项目显示在我的屏幕中心?

[英]How can I visually make my Selected Item in a CollectionView (Horizontal), show at the center of my screen?

I have a ListView with some items, and when I select one of those items, I am moved to a new page with a Horizontal CollectionView , with my SelectedItem as the item I selected on the page before.我有一个包含一些项目的ListView ,当我 select 其中一个项目时,我被移动到一个带有 Horizontal CollectionView的新页面,其中我的SelectedItem作为我之前在页面上选择的项目。 I have this part working.我有这部分工作。

My problem is, if the item I selected on the page before is too far to the right, (having to scroll to reach it) then my SelectedItem won't automatically show on screen.我的问题是,如果我之前在页面上选择的项目离右侧太远(必须滚动才能到达它),那么我的SelectedItem将不会自动显示在屏幕上。

Image with visible selected item:带有可见所选项目的图像:

Image where selected item is not visible:所选项目不可见的图像:

As you can see, when I selected the item "cat 7" it is not visible on the page.如您所见,当我选择项目“cat 7”时,它在页面上不可见。

My desired result is that, once an item is selected from the page before, it will always be visible immediately on the following page.我想要的结果是,一旦从之前的页面中选择了一个项目,它将始终立即在下一页上可见。

Does anybody know a way in which I can achieve this result?有人知道我可以达到这个结果的方法吗?

My xaml:我的 xaml:

    <CollectionView ItemsSource="{Binding CategorieList}"
                            ItemsLayout="HorizontalList"
                            x:Name="CategoryList"
                            VerticalOptions="Start"
                            Margin="0,22,0,0"
                            HeightRequest="32"
                            SelectionMode="Single"
                            SelectedItem="{Binding CategorieListSelectedItem, Mode=TwoWay}">
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup Name="CommonStates">
                                    <VisualState Name="Normal" />
                                    <VisualState Name="Selected">
                                        <VisualState.Setters>
                                            <Setter Property="BackgroundColor" Value="transparent" />
                                            <Setter TargetName="SelectedLabel" Property="Label.TextColor" Value="#004EFF"/>
                                            <Setter TargetName="SelectedLabel" Property="Label.FontSize" Value="22"/>
                                            <Setter TargetName="SelectedLabel" Property="Label.Margin" Value="20,-2,20,0"/>
                                        </VisualState.Setters>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                        
                        <Label Text="{Binding Name}"
                               x:Name="SelectedLabel"
                               Margin="20,0,20,0"
                               TextColor="#707070"
                               FontSize="20"
                               FontFamily="{StaticResource Helvetica}"/>
                        </Grid>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>

My ViewModel:我的视图模型:

    public Categories CategorieListSelectedItem
        {
            get
            {
                return categorieListSelectedItem;
            }
            set
            {
                categorieListSelectedItem = value;
                OnPropertyChanged("CategorieListSelectedItem");

                ProductList.Clear();
                if(categorieListSelectedItem.Products != null)
                {
                    foreach (var item in CategorieListSelectedItem.Products)
                    {
                        productList.Add(new ProductDetailsViewModel(item));                        
                    }
                }
                OnPropertyChanged("ProductList");

            }
        }

I am not sure how many Name in your CategorieList .我不确定您的CategorieList中有多少Name I use 8 for example.我以 8 为例。 When the count is not enough, the Name would show but not in the center.当计数不够时, Name会显示但不在中心。

public partial class Page3 : ContentPage
{
    public ObservableCollection<Categories> CategorieList { get; set; }
    public Categories SelectedCategorie { get; set; }
    public Page3()
    {
        InitializeComponent();
        CategorieList = new ObservableCollection<Categories>()
        {
            new Categories(){ Name="cat 1"},
            new Categories(){ Name="cat 2"},
            new Categories(){ Name="cat 3"},
            new Categories(){ Name="cat 4"},
            new Categories(){ Name="cat 5"},
            new Categories(){ Name="cat 6"},
            new Categories(){ Name="cat 7"},
            new Categories(){ Name="cat 8"},
            //new Categories(){ Name="cat 9"},
            //new Categories(){ Name="cat 10"},
            //new Categories(){ Name="cat 11"},
        };
        SelectedCategorie = CategorieList.Skip(6).FirstOrDefault();

        this.BindingContext = this;
    }
    protected override void OnAppearing()
    {

        CategoryList.ScrollTo(CategoryList.SelectedItem, null, ScrollToPosition.Center, false);

    }
}
public class Categories
{
    public string Name { get; set; }
}

SelectedItem is cat4 : SelectedItem 是cat4

 CategorieListSelectedItem = CategorieList.Skip(3).FirstOrDefault();

在此处输入图像描述

SelectedItem is cat7 : SelectedItem 是cat7

  CategorieListSelectedItem = CategorieList.Skip(6).FirstOrDefault();

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在另一页上显示我选择的项目? - How can i show my selected item on another page? 如何通过数据源的键或值更改当前选择的项目? - How can I change my current selected item by either the key or value of my datasource? 如何将我的 WinUI3 程序设置为在屏幕中央启动? - How can i set my WinUI3 program to be started in the center of the screen? 当我水平排列ListViewItem时,项目在VerticalAlignment上居中排列,如何设置顶部VerticalAlignment? - When I arrange ListViewItem horizontal, the item arrange center on VerticalAlignment, how can I set top VerticalAlignment? 如何使我的滚动视图/网格中的图像与正常尺寸的屏幕宽度匹配? - How can I make my image inside my scrollview/grid fit to the screen width with it's normal size? 如何在第一次加载页面时获取下拉列表中的所选项目? - How can I get the selected item of a dropdownlist, the first time my page loads? 我在Unity中没有语法错误,如何找到错误,以便了解为什么我的十字准线仍停留在屏幕中央? - I have no syntax errors in Unity, How can I find errors so I learn why my crosshair stays in the center of the screen? 如何在ListView中删除项目? - How I can remove a item in my ListView? 如何从表中选择通用列但确保我的键列也被选中? - How can i select generic columns from a table but make sure my keycolumn is selected too? 如果选择的值为null,如何使Linq选择返回值? - How can I make my Linq select return values if the value selected is null?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM