Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XAML字符串转换器到行定义和列定义?_C#_Wpf_Xaml_Uwp - Fatal编程技术网

C# XAML字符串转换器到行定义和列定义?

C# XAML字符串转换器到行定义和列定义?,c#,wpf,xaml,uwp,C#,Wpf,Xaml,Uwp,有人知道如何创建XAML字符串转换器,将字符串转换为Grid.RowDefinitions和Grid.ColumnDefinitions吗 例如: <Grid RowDefinition="auto,2*,2*" ColumnDefintions="auto,auto,*"> </Grid> 这将使XAML: <local:MegaGrid MegaRow="auto,2*,2*" MegaCol="auto,auto,*"> ... </lo

有人知道如何创建XAML字符串转换器,将字符串转换为Grid.RowDefinitions和Grid.ColumnDefinitions吗

例如:

<Grid RowDefinition="auto,2*,2*" ColumnDefintions="auto,auto,*">
</Grid>
这将使XAML:

<local:MegaGrid MegaRow="auto,2*,2*" MegaCol="auto,auto,*">
    ...
</local:MegaGrid>

如果您不需要在XAML中为MegaCol和MegaRow使用{Binding xxx}语句,那么最好使用普通属性,而不是更复杂的依赖属性

XAML:

C-UWP:

//All the Normal using statements plus a few more...
using System.Text.RegularExpressions;

using NamespaceOfYourApp {

    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;
            }
        }
    }

    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)
                {
                    GridLengthConverter converter = new GridLengthConverter();

                    RowDefinitions.Add(
                          new RowDefinition { Height = (GridLength)converter.ConvertFromString(item) }
                        );
                }
            }
        } // MegaRow

        private string zMegaCol = "";
    private object converter;

    public string MegaCol
        {
            get { return zMegaCol; }
            set
            {
                zMegaRow = value;
                ColumnDefinitions.Clear();

                string value2 = Regex.Replace(value, @"\s+", "");
                string[] items = value2.Split(',');

                GridLengthConverter converter = new GridLengthConverter();

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

    } // Class
} //NameSpaceOfYourApp

这两个属性都是不可绑定的,因此绑定转换器将不再有用。然而,这可能会有帮助:作为一个一般的暗示和荒谬的话,告诉一个人在这里呆了两年多,甚至自己写了答案,你也许应该考虑接受答案。请参见此处:提示:不要使用来自网格类的继承。相反,将此逻辑提取为一个行为类,这样您就可以将其附加到任何网格或相应的子体。我愿意接受您的注释,除非我不能对注释执行此操作
<local:MegaGrid MegaRow="auto,2*,2*" MegaCol="auto,auto,*">
    ...
</local:MegaGrid>
<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>
//All the Normal using statements plus a few more...
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;

using 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)
                {
                    GridLengthConverter converter = new GridLengthConverter();

                    RowDefinitions.Add(
                          new RowDefinition { Height = (GridLength)converter.ConvertFromString(item) }
                        );
                }
            }
        } // MegaRow

        private string zMegaCol = "";
    private object converter;

    public string MegaCol
        {
            get { return zMegaCol; }
            set
            {
                zMegaRow = value;
                ColumnDefinitions.Clear();

                string value2 = Regex.Replace(value, @"\s+", "");
                string[] items = value2.Split(',');

                GridLengthConverter converter = new GridLengthConverter();

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

    } // Class
} //NameSpaceOfYourApp
//All the Normal using statements plus a few more...
using System.Text.RegularExpressions;

using NamespaceOfYourApp {

    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;
            }
        }
    }

    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)
                {
                    GridLengthConverter converter = new GridLengthConverter();

                    RowDefinitions.Add(
                          new RowDefinition { Height = (GridLength)converter.ConvertFromString(item) }
                        );
                }
            }
        } // MegaRow

        private string zMegaCol = "";
    private object converter;

    public string MegaCol
        {
            get { return zMegaCol; }
            set
            {
                zMegaRow = value;
                ColumnDefinitions.Clear();

                string value2 = Regex.Replace(value, @"\s+", "");
                string[] items = value2.Split(',');

                GridLengthConverter converter = new GridLengthConverter();

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

    } // Class
} //NameSpaceOfYourApp