WPF Grid自定义行列时,边框的显示控制

作为一个接触WPF时间不太长的小菜鸟,今天也把学到的一点知识晒晒。新手们可以看看陈希章老师的文章:http://www.cnblogs.com/chenxizhang/archive/2011/09/22/2185414.html

陈老师的文章对我很有帮助,作为补充,写了下下面的例子:

xml部分:

 1 "VideoScreenTest.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         Title="MainWindow" Height="600" Width="800">
 5     "MainGrid" ShowGridLines="False">
 6         
 7             "0.2*"/>
 8             "0.2*"/>
 9             "0.2*"/>
10             "0.2*"/>
11             "0.2*"/>
12         
13         
14             "0.2*"/>
15             "0.2*"/>
16             "0.2*"/>
17             "0.2*"/>
18             "0.2*"/>
19         
20     
21 
22 
View Code

添加Border部分:

 1 /// 
 2         /// 添加Border
 3         /// 
 4         public void AddBorder()
 5         {
 6             int rows = MainGrid.RowDefinitions.Count;
 7             int columns = MainGrid.ColumnDefinitions.Count;
 8             TagButton btnTag = new TagButton();
 9             for (int i = 0; i < rows; i++)
10             {
11                 if (i != rows - 1)
12                 {
13                     #region
14 
15                     for (int j = 0; j < columns; j++)
16                     {
17                         Border border = null;
18                         if (j == columns - 1)
19                         {
20                             border = new Border()
21                             {
22                                 BorderBrush = new SolidColorBrush(Colors.Green),
23                                 BorderThickness = new Thickness(2.5, 2.5, 2.5, 0)
24                             };
25                         }
26                         else
27                         {
28                             border = new Border()
29                             {
30                                 BorderBrush = new SolidColorBrush(Colors.Green),
31                                 BorderThickness = new Thickness(2.5, 2.5, 0, 0)                                
32                             };
33                         }
34                         Grid.SetRow(border, i);
35                         Grid.SetColumn(border, j);
36                         MainGrid.Children.Add(border);
37                     }
38                     #endregion
39                 }
40                 else
41                 {
42                     for (int j = 0; j < columns; j++)
43                     {
44                         Border border = null;
45                         if (j + 1 != columns)
46                         {
47                             border = new Border
48                             {
49                                 BorderBrush = new SolidColorBrush(Colors.Green),
50                                 BorderThickness = new Thickness(2.5, 2.5, 0, 2.5)
51                             };
52                         }
53                         else
54                         {
55                             border = new Border
56                             {
57                                 BorderBrush = new SolidColorBrush(Colors.Green),
58                                 BorderThickness = new Thickness(2.5, 2.5, 2.5, 2.5)
59                             };
60                         }
61                         Grid.SetRow(border, i);
62                         Grid.SetColumn(border, j);
63                         MainGrid.Children.Add(border);
64                     }
65                 }
66             }
67         }
View Code

引用部分:

1  public MainWindow()
2         {
3             InitializeComponent();
4             AddBorder();
5         }
View Code

这样就不会出现Border边框重叠的问题了。

下面是显示效果:

WPF Grid自定义行列时,边框的显示控制_第1张图片

转载于:https://www.cnblogs.com/Raylin-lee/archive/2013/06/14/3135854.html

你可能感兴趣的:(WPF Grid自定义行列时,边框的显示控制)