繁体   English   中英

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

[英]programmatically setting grid width/height as a string, ie: “auto” or “*”, “10”, or “2*”, etc,,,

这是我想要做的:

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 对象的相同字符串到 RowDefinitions 转换器。 ColumnDefinitions 也是如此。 除了从 C# 而不是从 XAML 调用它。 我可以轻松编写 if else 语句和正则表达式来解析 XAML 将接受的 RowDefinition 和 ColumnDefinitions 字符串。 我只是想使用 XAML 在从字符串转换为这些对象时调用的 Grid 组件中已经内置的功能。

XAML 使用TypeConverters与字符串相互转换。 您可以获得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;
        }
    }
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM