XAML String转换为RowDefinitions和ColumnDefinitions?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了XAML String转换为RowDefinitions和ColumnDefinitions?相关的知识,希望对你有一定的参考价值。

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

例:

<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(", 	");

            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(", 	");

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

        }

    }

这将成为XAML:

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

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

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

以上是关于XAML String转换为RowDefinitions和ColumnDefinitions?的主要内容,如果未能解决你的问题,请参考以下文章

将选定的 PowerPoint 形状(或 DrawingML)转换为 XAML

如何将此.XAML代码转换为C#代码

通过 XAML 将图像设置为按钮背景

是否可以在 XAML 中的 TemplateBinding 上使用转换器?

C#/XAML - 字符串到图像源的转换

CAD转WPF: 关于CAD图纸文件转换为WPF矢量代码文件(xaml文件)的技巧