【问题标题】:XAML String Converter to RowDefinitions and ColumnDefinitions?XAML 字符串转换器到 RowDefinitions 和 ColumnDefinitions?
【发布时间】:2018-03-04 18:23:51
【问题描述】:

有人知道如何创建一个 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 的新 Grid 来实现,该 Grid 具有可以按上述设置的“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>

【问题讨论】:

  • 这两个属性都不可绑定,因此绑定转换器将没有用处。但是,这可能会有所帮助:rachel53461.wordpress.com/tag/grid
  • 作为一个一般性的提示(告诉一个在这里加入两年多,甚至自己写过答案的人,这是一个荒谬的提示),您或许应该考虑接受答案。见这里:What should I do when someone answers my question?
  • 附带提示:不要使用 Grid 类的继承。相反,将此逻辑提取为行为类,以便您可以将其附加到任何 Grid(或相应的后代)。
  • 我愿意接受你的评论,除非我不能这样做

标签: c# wpf xaml uwp


【解决方案1】:

如果您不需要在 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-10
    相关资源
    最近更新 更多