none
如何用代码添加一个GRID RRS feed

  • 问题

  • 我想在<StackPanel>中添加一个GRID,这个GRID分成几行几列请问如何用代码添加?如下一个结构如何用代码添加?

    <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"></ColumnDefinition>
                        <ColumnDefinition Width="Auto"></ColumnDefinition>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                        <ColumnDefinition Width="Auto"></ColumnDefinition>
                    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
                <RowDefinition Height="1"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>

    <Grid>


    菜爷
    2011年3月1日 3:56

答案

  • 以下是经过测试通过的代码

    应该完全满足你的要求。

    ========================

            /// <summary>
            /// 方法1 - [纯C#]代码加入
            /// </summary>
            /// <param name="OwnerPannel"></param>
            private void AddCustGrid1(Panel OwnerPannel)
            {
                var GrdToAdd = new Grid();
                GrdToAdd.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Auto) });
                GrdToAdd.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Auto) });
                GrdToAdd.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
                GrdToAdd.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Auto) });
                GrdToAdd.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Pixel), });
                GrdToAdd.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto), });
                GrdToAdd.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star), });
                OwnerPannel.Children.Add(GrdToAdd);
                //AddCustGridResultTest(GrdToAdd);
            }
            /// <summary>
            /// 方法2 - [Xaml + C#]代码加入
            /// </summary>
            /// <param name="OwnerPannel"></param>
            private void AddCustGrid2(Panel OwnerPannel)
            {
                var Xaml =
                    @"<Grid" +
                    @"    xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""" +
                    @"    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">" +
                    @"    <Grid.ColumnDefinitions>" +
                    @"        <ColumnDefinition" +
                    @"            Width=""Auto""></ColumnDefinition>" +
                    @"        <ColumnDefinition" +
                    @"            Width=""Auto""></ColumnDefinition>" +
                    @"        <ColumnDefinition" +
                    @"            Width=""*""></ColumnDefinition>" +
                    @"        <ColumnDefinition" +
                    @"            Width=""Auto""></ColumnDefinition>" +
                    @"    </Grid.ColumnDefinitions>" +
                    @"    <Grid.RowDefinitions>" +
                    @"        <RowDefinition" +
                    @"            Height=""1""></RowDefinition>" +
                    @"        <RowDefinition" +
                    @"            Height=""Auto""></RowDefinition>" +
                    @"        <RowDefinition" +
                    @"            Height=""*""></RowDefinition>" +
                    @"    </Grid.RowDefinitions>" +
                    @"</Grid>";
                var GrdToAdd = XamlReader.Load(Xaml) as Grid;
                OwnerPannel.Children.Add(GrdToAdd);
                //AddCustGridResultTest(GrdToAdd);
            }
            // 加入几个Button到这个Grid,
            // 测试一下看看究竟这个Grid是不是符合你的要求!
            private void AddCustGridResultTest(Grid GridAdded)
            {
                for (var I = 0; I < GridAdded.RowDefinitions.Count; I++)
                {
                    for (var J = 0; J < GridAdded.ColumnDefinitions.Count; J++)
                    {
                        var Txt = "行{I},列{J}";
                        Txt = Txt.Replace("{I}", I.ToString());
                        Txt = Txt.Replace("{J}", J.ToString());
                        var ElButton = new Button() { Content = Txt, };
                        ElButton.SetValue(Grid.RowProperty, I);
                        ElButton.SetValue(Grid.ColumnProperty, J);
                        GridAdded.Children.Add(ElButton);
                    }
                }
            }

    • 已标记为答案 菜爷 2011年3月1日 14:17
    2011年3月1日 7:50

全部回复

  • 以下是经过测试通过的代码

    应该完全满足你的要求。

    ========================

            /// <summary>
            /// 方法1 - [纯C#]代码加入
            /// </summary>
            /// <param name="OwnerPannel"></param>
            private void AddCustGrid1(Panel OwnerPannel)
            {
                var GrdToAdd = new Grid();
                GrdToAdd.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Auto) });
                GrdToAdd.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Auto) });
                GrdToAdd.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
                GrdToAdd.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Auto) });
                GrdToAdd.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Pixel), });
                GrdToAdd.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto), });
                GrdToAdd.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star), });
                OwnerPannel.Children.Add(GrdToAdd);
                //AddCustGridResultTest(GrdToAdd);
            }
            /// <summary>
            /// 方法2 - [Xaml + C#]代码加入
            /// </summary>
            /// <param name="OwnerPannel"></param>
            private void AddCustGrid2(Panel OwnerPannel)
            {
                var Xaml =
                    @"<Grid" +
                    @"    xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""" +
                    @"    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">" +
                    @"    <Grid.ColumnDefinitions>" +
                    @"        <ColumnDefinition" +
                    @"            Width=""Auto""></ColumnDefinition>" +
                    @"        <ColumnDefinition" +
                    @"            Width=""Auto""></ColumnDefinition>" +
                    @"        <ColumnDefinition" +
                    @"            Width=""*""></ColumnDefinition>" +
                    @"        <ColumnDefinition" +
                    @"            Width=""Auto""></ColumnDefinition>" +
                    @"    </Grid.ColumnDefinitions>" +
                    @"    <Grid.RowDefinitions>" +
                    @"        <RowDefinition" +
                    @"            Height=""1""></RowDefinition>" +
                    @"        <RowDefinition" +
                    @"            Height=""Auto""></RowDefinition>" +
                    @"        <RowDefinition" +
                    @"            Height=""*""></RowDefinition>" +
                    @"    </Grid.RowDefinitions>" +
                    @"</Grid>";
                var GrdToAdd = XamlReader.Load(Xaml) as Grid;
                OwnerPannel.Children.Add(GrdToAdd);
                //AddCustGridResultTest(GrdToAdd);
            }
            // 加入几个Button到这个Grid,
            // 测试一下看看究竟这个Grid是不是符合你的要求!
            private void AddCustGridResultTest(Grid GridAdded)
            {
                for (var I = 0; I < GridAdded.RowDefinitions.Count; I++)
                {
                    for (var J = 0; J < GridAdded.ColumnDefinitions.Count; J++)
                    {
                        var Txt = "行{I},列{J}";
                        Txt = Txt.Replace("{I}", I.ToString());
                        Txt = Txt.Replace("{J}", J.ToString());
                        var ElButton = new Button() { Content = Txt, };
                        ElButton.SetValue(Grid.RowProperty, I);
                        ElButton.SetValue(Grid.ColumnProperty, J);
                        GridAdded.Children.Add(ElButton);
                    }
                }
            }

    • 已标记为答案 菜爷 2011年3月1日 14:17
    2011年3月1日 7:50
  • 你真是救星。到广州来的时候,我请你吃饭。


    菜爷
    2011年3月1日 14:18