我在borderbrush上使用了visualbrush来为每个边框方向设置不同的颜色。
<Border Grid.Row="0" Grid.Column="4" BorderThickness="10,10,5,5" CornerRadius="0" HorizontalAlignment="Right" Height="50" Width="50" VerticalAlignment="Bottom" >
<Border.BorderBrush>
<VisualBrush>
<VisualBrush.Visual>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="10"/>
<RowDefinition Height="30"/>
<RowDefinition Height="10"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="10"/>
</Grid.ColumnDefinitions>
<Border Background="Blue" Grid.Row="1" Grid.Column="0"/>
<Border Background="Red" Grid.Row="1" Grid.Column="2"/>
<Border Background="Green" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3"/>
<Border Background="Yellow" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3"/>
</Grid>
</VisualBrush.Visual>
</VisualBrush>
</Border.BorderBrush>
</Border>
在xaml上,此代码运行良好。每个边框方向的颜色都不同。 但是在代码后面,
Grid grid = new Grid();
grid.Height = this.rowHeight[r] + topHeight + bottomHeight;
grid.Width = this.columnWidth[c] + leftWidth + rightWidth;
grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(topHeight) });
grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(rowHeight[r])});
grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(bottomHeight) });
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(leftWidth) });
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(columnWidth[c])});
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(rightWidth) });
Border bdTop = new Border() { Background = new SolidColorBrush(cellInfo["BorderBrush"]["Top"]) };
Border bdBottom = new Border() { Background = new SolidColorBrush(cellInfo["BorderBrush"]["Bottom"]) };
Border bdLeft = new Border() { Background = new SolidColorBrush(cellInfo["BorderBrush"]["Left"]) };
Border bdRight = new Border() { Background = new SolidColorBrush(cellInfo["BorderBrush"]["Right"]) };
bdTop.Height = topHeight;
bdBottom.Height = bottomHeight;
bdLeft.Height = this.rowHeight[r];
bdRight.Height = this.rowHeight[r];
grid.Children.Add(bdTop);
grid.Children.Add(bdBottom);
grid.Children.Add(bdLeft);
grid.Children.Add(bdRight);
Grid.SetRow(bdTop, 0);
Grid.SetColumn(bdTop, 0);
Grid.SetColumnSpan(bdTop, 3);
Grid.SetRow(bdBottom, 2);
Grid.SetColumn(bdBottom, 0);
Grid.SetColumnSpan(bdBottom, 3);
Grid.SetRow(bdLeft, 1);
Grid.SetColumn(bdLeft, 0);
Grid.SetRow(bdRight, 1);
Grid.SetColumn(bdRight, 2);
VisualBrush vb = new VisualBrush();
vb.Visual = grid;
如果所有厚度都不相同,则在代码后面,每个边框方向将使用每个画笔。边框顶部显示整个画笔。边框底部显示了整个画笔。
我不知道出了什么问题。...
边框UI中使用的xaml代码。在表格单元格边框中使用的代码后面。
答案 0 :(得分:0)
如果您想使边框具有4种不同的颜色,可以将它们彼此层叠,实现起来更加简单:
<Grid Height="50" Width="50">
<Border BorderBrush="Blue" BorderThickness="5,0,0,0"></Border>
<Border BorderBrush="Red" BorderThickness="0,0,5,0"></Border>
<Border BorderBrush="Yellow" BorderThickness="0,0,0,5"></Border>
<Border BorderBrush="Green" BorderThickness="0,5,0,0"></Border>
<Grid Margin="5">
<!-- Content here -->
</Grid>
</Grid>