以编程方式将网格宽度/高度设置为字符串,即:"auto"或"*"、"10"或"2*"等,,,



这是我正在尝试做的事情:

XAML:

<local:MegaGrid MegaCol="auto,auto,*,auto">
    <Button Grid.Column="0" Content="Btn1"/>
    <Button Grid.Column="1" Content="Btn2"/>
    <Button Grid.Column="2" Content="Btn3 Stretch"    
                            HorizontalAlignment="Stretch"/>
    <Button Grid.Column="3" Content="Btn2"/>
</local:MegaGrid>

C#:

//All the Normal using statements plus a few more...
using System.Text.RegularExpressions;
//Uncomment these two for WPF or comment out for UWP
//using System.Windows;
//using System.Windows.Controls;
namespace NameSpaceOfYourApp {
public class MegaGrid : Grid
{
    private string zMegaRow = "";
    public string MegaRow
    {
        get { return zMegaRow; }
        set
        {
            zMegaRow = value;
            RowDefinitions.Clear();
            string value2 = Regex.Replace(value, @"s+", "");
            string[] items = value2.Split(',');
            foreach (string item in items)
            {
                // QUESTION: HOW TO CONVERT ITEM
                // DIRECTLY FROM STRING INTO RowDefinition?
                // Without Parsing the string
                if (item == "*")
                {
                    RowDefinitions.Add(
                      new RowDefinition { 
                        Height = new GridLength(1, GridUnitType.Star) }
                    );
                }
                else if (item == "auto")
                {
                    RowDefinitions.Add(
                       new RowDefinition { Height = GridLength.Auto }
                    );
                }
            }
        }
    } // MegaRow
    private string zMegaCol = "";
    public string MegaCol
    {
        get { return zMegaCol; }
        set
        {
            zMegaRow = value;
            ColumnDefinitions.Clear();
            string value2 = Regex.Replace(value, @"s+", "");
            string[] items = value2.Split(',');
            foreach (string item in items)
            {
                // QUESTION: HOW TO CONVERT ITEM
                // DIRECTLY FROM STRING INTO ColumnDefinition?
                // Without Parsing the string
                if (item == "*")
                {
                    ColumnDefinitions.Add(
                      new ColumnDefinition { 
                        Width = new GridLength(1, GridUnitType.Star) }
                    );
                }
                else if (item == "auto")
                {
                    ColumnDefinitions.Add(
                       new ColumnDefinition { Width = GridLength.Auto }
                    );
                }
            }
        }
    } // MegaCol
} //MegaGrid
} //NameSpaceOfYourApp

我需要知道的是,如何调用 XAML 用于创建 RowDefintions 对象的相同字符串到行定义转换器。 列定义也是如此。除了从 C# 而不是从 XAML 调用它。我可以轻松地编写 if else 语句和正则表达式来解析 XAML 将接受的 RowDefinition 和 ColumnDefinition 的字符串。 我只是尝试使用已经内置到 Grid 组件中的函数,XAML 在从字符串转换为这些对象时调用这些函数。

XAML 使用类型转换器与字符串进行转换。您可以像这样获取GridLength(或任何其他类型(的TypeConverter

var converter = TypeDescriptor.GetConverter(typeof(GridLength));

然后,您可以在 foreach 循环中使用 TypeConverter.ConvertFromString 方法,如下所示:

foreach (string item in items)
{
    ColumnDefinitions.Add(
        new ColumnDefinition { 
            Width = (GridLength)converter.ConvertFromString(item)
        }
    );
}

以上是针对您的列的,行的代码看起来是一样的。

Dave M的完整答案:

foreach (string item in items)
{
    GridLengthConverter converter = new GridLengthConverter();
    RowDefinitions.Add(
      new RowDefinition { 
        Height = (GridLength)converter.ConvertFromString(item) }
        );
}

上述答案适用于 WPF。 如果你使用的是 UWP,则可以通过创建自己的 GridLengthConverter 类来使用上面的代码,因为此类不随 UWP 一起提供:

public class GridLengthConverter
{
    public GridLength ConvertFromString(string s)
    {
        if (s == "auto")
            return GridLength.Auto;
        else if (s == "*")
            return new GridLength(1, GridUnitType.Star);
        else
        {
            int pixels;
            int.TryParse(s, out pixels);
            var g = new GridLength(pixels);
            return g;
        }
    }
}

最新更新