在 ViewCell 中共享网格列的宽度? [英] Share width of a Grid-Column in a ViewCell?

查看:22
本文介绍了在 ViewCell 中共享网格列的宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的 ListViews ViewCell 中的列的大小都相同.它当前设置为自动,最宽的名称应该获胜,所有其他列应设置为最宽的标签宽度.目前,它们每行都有不同的宽度.

I would like to have my columns in my ListViews ViewCell to be all of the same size. It is currently set to auto and the widest name should win and all other columns should be set to the widest labels width. Currently they have all different widths in each row.

ListView ItemTemplate 示例:

<ViewCell>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" /> /
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Label Grid.Row="0" Text="{Binding Name}"/>
[....]

在 WPF 中,我们有 SharedSizeGroup 属性,Xamarin.Forms 中是否有类似的东西?有没有不进行过多黑客攻击的解决方法?

In WPF we have the property SharedSizeGroup, is there something similar in Xamarin.Forms? Is there a workaround without hacking too much?

推荐答案

很遗憾,没有 - 没有这样的属性.

Unfortunately, no - there is no such property.

这是一件相当困难的事情,因为您需要先测量所有行,然后才能确定哪一行的文本最宽.没有跨平台的方法可以做到这一点.在 Android 和 iOS 的平台级别有办法做到这一点,但我认为 WinPhone 没有办法.

This is a rather difficult thing to do since you need to measure all rows before you can figure out which one has the widest text. There is not a cross-platform way to do that. There are ways to do it at the platform level for Android and iOS, but I don't think there is a way for WinPhone.

:

另一种方法是使用 OneWayToSource 绑定并跟踪每个列表视图行的 ColumnDefinition 条目.

An alternative is to use OneWayToSource bindings and keeping track of the ColumnDefinition entries for each listview row.

XamlPage.Xaml:

XamlPage.Xaml:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="FormsSandbox.XamlPage">
    <ContentPage.Padding>
        <OnPlatform x:TypeArguments="Thickness" iOS="0,20,0,0" Android="0" WinPhone="0"/>
    </ContentPage.Padding>

    <AbsoluteLayout>
        <ListView x:Name="MyLV" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid RowSpacing="0" ColumnSpacing="0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Label Grid.Column="0" Text="{Binding .}" MinimumWidthRequest="50" HorizontalOptions="StartAndExpand" SizeChanged="LabelSizeChanged" />
                            <Label Grid.Column="1" Text="Second Column"/>
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </AbsoluteLayout>

</ContentPage>

XamlPage.xaml.cs:

XamlPage.xaml.cs:

using System;
using Xamarin.Forms;
using System.Collections.Generic;

namespace FormsSandbox
{
    public partial class XamlPage : ContentPage
    {
        private double _colSize = 0.0;
        private List<ColumnDefinition> _columns = new List<ColumnDefinition>();

        public XamlPage ()
        {
            InitializeComponent ();

            var data = new List<string> ();

            data.Add ("Lorem ipsum");
            data.Add ("Foo");
            data.Add ("Dolor semet");
            data.Add ("Test");
            data.Add (".");
            data.Add ("Xamarin Forms Is Great");
            data.Add ("Short");
            data.Add ("Longer than Short");
            data.Add ("");
            data.Add ("Hyphenated");
            data.Add ("Non-hyphenated");
            data.Add ("Ironic, eh?");

            MyLV.ItemsSource = data;
        }

        public void LabelSizeChanged (object sender, EventArgs e)
        {
            var label = (Label)sender;
            var grid = (Grid)label.Parent;
            var column = grid.ColumnDefinitions [0];
            if (!_columns.Contains (column)) {
                _columns.Add (column);
            }
            var adjustments = new List<ColumnDefinition> ();
            if (label.Width > _colSize) {
                _colSize = label.Width;
                adjustments.AddRange (_columns);
            } else {
                adjustments.Add (column);
            }
            foreach (var col in adjustments) {
                col.Width = new GridLength (_colSize);
            }
        }
    }
}

这篇关于在 ViewCell 中共享网格列的宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆