【问题标题】:Edit children placement in rows and columns directly from the object definition直接从对象定义编辑子元素在行和列中的位置
【发布时间】:2017-12-31 03:27:08
【问题描述】:

我有以下对象:

new Grid {
    RowDefinitions =
    {
        new RowDefinition { Height = new GridLength(3, GridUnitType.Star) },
        new RowDefinition { Height = new GridLength(2, GridUnitType.Star) },
        new RowDefinition { Height = new GridLength(2, GridUnitType.Star) }
    },
    ColumnDefinitions =
    {
        new ColumnDefinition { Width = new GridLength(3, GridUnitType.Star) },    
        new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }
    },
    Children =
    {
        new Label { Text = "TRIP #5", FontFamily = Device.OnPlatform(null, "latoblack.ttf#Lato Black", null), FontSize = 16, TextColor = Color.White, HorizontalTextAlignment = TextAlignment.Start, Margin = new Thickness(0, 0, 0, 10) },
        new Label { Text = "Started 24/07/2017", FontFamily = Device.OnPlatform(null, "latolight.ttf#Lato Light", null), FontSize = 16, TextColor = Color.White, HorizontalTextAlignment = TextAlignment.Start },
        new Label { Text = "Is currently in progress", FontFamily = Device.OnPlatform(null, "latolight.ttf#Lato Light", null), FontSize = 16, TextColor = Color.White, HorizontalTextAlignment = TextAlignment.Start }
    }
}

我想将标签放置在 (0,0)、(0,1)、(1,1) 位置,而不必使用 Children.Add。更具体地说,我想将标签的位置设置为我直接从对象定义中提到的位置。我该怎么做?

提前谢谢你!

【问题讨论】:

标签: c# xamarin xamarin.forms


【解决方案1】:

您可以像这样使用Grid 静态方法Grid.SetRow, Grid.SetColumn 等:

        var label = new Label { Text = "TRIP #5", FontSize = 16, TextColor = Color.Black, HorizontalTextAlignment = TextAlignment.Start, Margin = new Thickness(0, 0, 0, 10) };
        var grid = new Grid {
            RowDefinitions =
            {
                new RowDefinition { Height = new GridLength(3, GridUnitType.Star) },
                new RowDefinition { Height = new GridLength(2, GridUnitType.Star) },
                new RowDefinition { Height = new GridLength(2, GridUnitType.Star) }
            },
            ColumnDefinitions =
            {
                new ColumnDefinition { Width = new GridLength(3, GridUnitType.Star) },    
                new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }
            },
            Children =
            {
                label,
            }
        };

        grid.Padding = new Thickness(0,20,0,0);


        Grid.SetRow(label, 0);
        Grid.SetColumn(label, 0);
        Grid.SetColumnSpan(label,2);
        Grid.SetRowSpan(label, 3);

        this.Content = grid;

【讨论】:

  • 有没有什么方法可以从 Grid 对象内部设置行/列位置?
  • 嗯,另一种方式是myGrid.Children.Add(new Label { Text = "Row 1" }, 0, 0);,但这显然是你不想使用的:)
猜你喜欢
  • 1970-01-01
  • 2011-01-15
  • 2019-08-08
  • 2013-05-12
  • 1970-01-01
  • 1970-01-01
  • 2011-03-19
  • 2021-05-03
  • 1970-01-01
相关资源
最近更新 更多