XAML String转换为RowDefinitions和ColumnDefinitions?

时间:2018-03-04 18:23:51

标签: c# wpf xaml uwp

有人知道如何创建一个可以将字符串转换为Grid.RowDefinitions和Grid.ColumnDefinitions的XAML字符串转换器吗?

示例:

<Grid RowDefinition="auto,2*,2*" ColumnDefintions="auto,auto,*">
</Grid>

而不是:

<Grid>   
    <Grid.RowDefinitions> 
        <RowDefinition Height="auto"/>
        <RowDefinition Height="2*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions> 
        <ColumnDefinitions Width="auto"/>
        <ColumnDefinitions Width="auto"/>
        <ColumnDefinitions Width="*"/>
    </Grid.Columndefintions>

    <!-- ahh...my fingers are so tired of typing XAML at this point!!! 
        -->  
    </Grid>

这里有一些代码几乎可以通过创建一个名为MegaGrid的新网格来实现,其中“MegaRow”和“MegaCol”属性可以如上设置......目前还不确定如何完成它。

public class MegaGrid : Grid
{
        public static readonly DependencyProperty MegaRowProperty
            = DependencyProperty.RegisterAttached(
                "MegaRow", typeof(string), 
                typeof(MegaGrid),
                new PropertyMetadata(-1, MegaRowChanged)
                );

        public static void MegaRowChanged(
            DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            if (!(obj is MegaGrid))
                return;

            MegaGrid grid = (MegaGrid)obj;
            grid.RowDefinitions.Clear();

            string   value = (string) e.NewValue;
            string[] items = value.Split(", \t");

            foreach (string item in items)
            {
                //??? grid.RowDefinitions.Add(new RowDefinition { Height = });
            }
        }

        public static readonly DependencyProperty MegaColProperty
            = DependencyProperty.RegisterAttached(
                "MegaCol", typeof(string),
                typeof(MegaGrid),
                new PropertyMetadata(-1, MegaColChanged)
                );

        public static void MegaColChanged(
            DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            if (!(obj is MegaGrid))
                return;

            MegaGrid grid = (MegaGrid)obj;
            grid.ColumnDefinitions.Clear();

            string value = (string)e.NewValue;
            string[] items = value.Split(", \t");

            foreach (string item in items)
            {
                //??? grid.ColumnDefinitions.Add(new ColumnDefinition { Width = });
            }

        }

    }

这将成为XAML:

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

1 个答案:

答案 0 :(得分:-1)

如果您不需要使用&#34; {Binding xxx}&#34;对于XAML中MegaCol和MegaRow的声明,最好使用普通属性而不是更复杂的依赖属性。

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#-WPF:

//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

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