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

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

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

xml部分:

 1 <Window x:Class="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     <Grid Name="MainGrid" ShowGridLines="False">
 6         <Grid.RowDefinitions>
 7             <RowDefinition Height="0.2*"/>
 8             <RowDefinition Height="0.2*"/>
 9             <RowDefinition Height="0.2*"/>
10             <RowDefinition Height="0.2*"/>
11             <RowDefinition Height="0.2*"/>
12         </Grid.RowDefinitions>
13         <Grid.ColumnDefinitions>
14             <ColumnDefinition Width="0.2*"/>
15             <ColumnDefinition Width="0.2*"/>
16             <ColumnDefinition Width="0.2*"/>
17             <ColumnDefinition Width="0.2*"/>
18             <ColumnDefinition Width="0.2*"/>
19         </Grid.ColumnDefinitions>
20     </Grid>
21 
22 </Window>
View Code

添加Border部分:

 1 /// <summary>
 2         /// 添加Border
 3         /// </summary>
 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边框重叠的问题了。

下面是显示效果:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值