c# - 如何使用 Border() 仅显示网格(而非列)的行边框

标签 c# .net silverlight border silverlight-5.0

我在 silverlight 应用程序中使用 c#。

我有一个 1 行 3 列的网格。 有两件事我不知道该怎么做:

(1) 我必须显示行的边界(不是列,只有行)。怎么做?

目前我有这样的网格:

//The p in function call below is yhe object obtained on deserialixing xml.
     private  static Grid GenerateGrid(Parameters p) 
            {
                Grid myGrid = new Grid();
                myGrid.Width = 650;       
                myGrid.HorizontalAlignment = HorizontalAlignment.Left;
                myGrid.VerticalAlignment = VerticalAlignment.Top;
                myGrid.ShowGridLines = false;

                ColumnDefinition colDef1 = new ColumnDefinition();
                ColumnDefinition colDef2 = new ColumnDefinition();
                ColumnDefinition colDef3 = new ColumnDefinition();
                myGrid.ColumnDefinitions.Add(colDef1);
                myGrid.ColumnDefinitions.Add(colDef2);
                myGrid.ColumnDefinitions.Add(colDef3);

                int totalRows = p.Parameter.Count() + p.Separator.Count();

                for (int i = 0; i < totalRows; i++)
                {
                    myGrid.RowDefinitions.Add(new RowDefinition());
                }               
                return (myGrid);
            } 

调用这个函数是:

 XmlParameterClasses.Parameters parameter = 
     (XmlParameterClasses.Parameters)deserializer.Deserialize(reader);
 Grid BigGrid = GenerateGrid(parameter); 

我的尝试是这样的:(我用 Border 来做这个,见函数末尾)

        private static Grid GenerateComboBox(ViewModel.XmlParameterClasses.Parameter param, int LoopCount, Grid g) 
        { //param is the object of the class Parameter
            StackPanel sp1 = new StackPanel(); //These three stackpanels are inside the grid cell
            StackPanel sp2 = new StackPanel();
            StackPanel sp3 = new StackPanel();
            ComboBox cb = new ComboBox();
            TextBlock txtblk1 = new TextBlock();
            TextBlock txtblkLabel = new TextBlock();

            ////////////////////////////////////          

            //Label Display
            txtblkLabel.Text = param.Label;
            txtblkLabel.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            txtblkLabel.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            txtblkLabel.TextAlignment = System.Windows.TextAlignment.Center;
            txtblkLabel.FontWeight = FontWeights.Bold;
            txtblkLabel.FontSize = 15;
            txtblkLabel.FontStyle = FontStyles.Normal;
            txtblkLabel.Padding = new Thickness(5, 10, 5, 10);

            sp1.Orientation = Orientation.Horizontal;
            sp1.Children.Add(txtblkLabel);
            sp1.Width = 100;
            sp1.Height = 50;
            Grid.SetRow(sp1, LoopCount);
            Grid.SetColumn(sp1, 0);
            g.Children.Add(sp1);

            foreach(var item in param.Component.Attributes.Items) {
                cb.Items.Add(item);
            }
            cb.SelectionChanged += new SelectionChangedEventHandler(comboBox1_SelectionChanged);
            cb.SelectedIndex = cb.Items.Count - 1;



            //For text Display
            txtblk1.Text = cb.SelectedValue.ToString() + " millions";
            txtblk1.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            txtblk1.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            txtblk1.TextAlignment = System.Windows.TextAlignment.Center;
            txtblk1.FontWeight = FontWeights.Bold;
            txtblk1.FontSize = 15;
            txtblk1.FontStyle = FontStyles.Normal;
            txtblk1.Padding = new Thickness(5, 10, 5, 10);

            sp2.Orientation = Orientation.Horizontal;
            sp2.Children.Add(txtblk1);
            Grid.SetColumn(sp2, 2);
            Grid.SetRow(sp2, LoopCount);
            g.Children.Add(sp2);


            //For combo box display
            cb.Width = 45;
            cb.Height = 25;
            sp3.Orientation = Orientation.Horizontal;
            sp3.Children.Add(cb);
            sp3.Width = 50;
            sp3.Height = 50;

            Grid.SetColumn(sp3, 1);
            Grid.SetRow(sp3, LoopCount);
            g.Children.Add(sp3);


 ////////////Here is the Border Display ////////////////////////////
            Border rect = new Border();
            rect.Width = g.Width;
            rect.Height = g.Height;
            rect.BorderThickness = new Thickness(5);
            rect.BorderBrush = new SolidColorBrush(Colors.Black);
            g.Children.Add(rect);
////////////////////////////////////////////////////////////////////
            return (g);
        }

但获得的输出是这样的:(它只覆盖第一个单元格的边框而不是该行中的其他两个单元格,而我只想在一行上有一个边框(不在该行的列上,只是行边界) ) enter image description here 有人可以帮助我实现这一步吗? 是否有可能实现我想尝试做的事情?

注意:请注意,代码必须仅使用 c# 而非 xaml 实现。

最佳答案

我是通过创建一个由 1 列和行组成的网格来完成的(而不是 3*3 单元格,它必须是 1*3(行*列))。然后在每一行中创建边框,然后再次创建具有 1 行和 3 列的网格,然后创建这个小网格的边框。

代码是:

            Border rect = new Border();
            rect.Width = g.Width;
            rect.Height = g.Height;
            rect.BorderThickness = new Thickness(2);
            rect.BorderBrush = new SolidColorBrush(Colors.Black);        

            Grid childGrid = new Grid();
            ColumnDefinition colDef1 = new ColumnDefinition();
            ColumnDefinition colDef2 = new ColumnDefinition();
            ColumnDefinition colDef3 = new ColumnDefinition();
            childGrid.ColumnDefinitions.Add(colDef1);
            childGrid.ColumnDefinitions.Add(colDef2);
            childGrid.ColumnDefinitions.Add(colDef3);
            TextBlock txtblk3 = new TextBlock();
            var border = new Border()
            {
                Background = new SolidColorBrush(Colors.LightGray)
            };
            border.Height = 14;

            var border1 = new Border()
            {
                Background = new SolidColorBrush(Colors.White)
            };
            border1.Height = 14;

            Grid.SetColumnSpan(border, 3);
            Grid.SetRow(childGrid, LoopCount);
            childGrid.Children.Add(border);

            txtblk3.FontSize = 14;
            txtblk3.FontWeight = FontWeights.Bold;

            txtblk3.Text = param.Separator[SeparatorPosition];
            Grid.SetColumn(border1, 1);
            Grid.SetRow(border1,LoopCount);
            border1.Child = txtblk3;    

            childGrid.Children.Add(border1);
            g.Children.Add(childGrid);
            return (g);

其中“g”只有 1 列和“LoopCount”行数。它对我有用。

关于c# - 如何使用 Border() 仅显示网格(而非列)的行边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23869830/

相关文章:

.net Forms 身份验证 - 手动设置 HttpContext.Current.User 在自定义 AuthorizeAttribute 中不起作用

silverlight - 如何将KeyUp/down绑定(bind)到按钮Click事件?

c# - 当我以编程方式将段添加到 Silverlight 2 中的路径时,为什么我的路径不显示?

c# - 如何将 double[] 转换为 System.Windows.Media.DoubleCollection?

c# - CosmosDB 查询性能

c# - 如何在聚合之前将值转换为 long ?

c# - 如何动态加载程序集到当前应用程序域到c#项目?

c# - 从具有 IList 返回类型的方法返回 ReadOnlyCollection

c# - 花括号在 Xpath 中有什么作用?

c# - 使用 Swig 为 C++ 代码构建 C# 包装器时,是否可以向现有方法添加代码?