作为一个接触WPF时间不太长的小菜鸟,今天也把学到的一点知识晒晒。新手们可以看看陈希章老师的文章:http://www.cnblogs.com/chenxizhang/archive/2011/09/22/2185414.html
陈老师的文章对我很有帮助,作为补充,写了下下面的例子:
xml部分:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
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>
添加Border部分:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
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 }
引用部分:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 public MainWindow() 2 { 3 InitializeComponent(); 4 AddBorder(); 5 }
这样就不会出现Border边框重叠的问题了。
下面是显示效果: