自定义背景:前面讲过自定义圆角按钮和圆形按钮,这节主要将自定义背景色。
在以往的设置中我们的CornerRadius是一个值,
<Border Height="100" Width="200" CornerRadius="25" BorderBrush="#49B7A3" Padding="0" BorderThickness="1" >
<Button Height="100" Width="200" Background ="Transparent" BorderThickness="0"/>
</Border>
效果图:
然而实际上是可以设置四个值的,如果设置的是一个值,其实表示的是四个值是相同的,效果图同上。
如何设置不同的区域背景图呢,下面我先将效果图进行展示在进一步分析:
代码如下:
<Window x:Class="DataGrid.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Icon ="http://metro.mahapps.com/winfx/xaml/iconpacks"
xmlns:local="clr-namespace:DataGrid"
mc:Ignorable="d"
Title="Window1" Height="450" Width="800">
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVis" />
</Window.Resources>
<Border CornerRadius="30" Background="#EFF2F7" MouseDown="Border_MouseDown" MouseLeftButtonDown="Border_MouseLeftButtonDown">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<!--Left Menu-->
<Border Background="#623ED0" CornerRadius="0 50 0 30">
<Grid>
</Grid>
</Border>
<!--Main Section-->
<Border Grid.Column="1" Background="Aquamarine" CornerRadius="5">
<Grid Grid.Column="1" Margin="30 20 20 20">
</Grid>
</Border>
</Grid>
</Border>
</Window>
解析:主要是想做一个主界面,左边是菜单右边是主页内容。
考略到左右分割就是两列,因此我选择了<grid>图表控件,创建了两个 <ColumnDefinition>;其中左边是绝对长度200,其余是右边。
左边当时想做一个不规则图形,因此在左边用<Border>包裹一个<Grid>,方便后期修改,可以看到右上角和左下角都有个倒角,但是倒角的大小不一样;右边用<Border>包裹一个<Grid>方便后期放自定义控件。
<Border>中CornerRadius的顺序是左上、右上、右下、左下。感兴趣的话可以自己实验一下。
源码:
using System.Windows.Media;
using MS.Internal;
using MS.Internal.PresentationFramework;
using MS.Internal.Telemetry.PresentationFramework;
namespace System.Windows.Controls
{
public class Border : Decorator
{
private struct Radii
{
internal double LeftTop;
internal double TopLeft;
internal double TopRight;
internal double RightTop;
internal double RightBottom;
internal double BottomRight;
internal double BottomLeft;
internal double LeftBottom;
internal Radii(CornerRadius radii, Thickness borders, bool outer)
{
double num = 0.5 * borders.Left;
double num2 = 0.5 * borders.Top;
double num3 = 0.5 * borders.Right;
double num4 = 0.5 * borders.Bottom;
if (outer)
{
if (DoubleUtil.IsZero(radii.TopLeft))
{
LeftTop = (TopLeft = 0.0);
}
else
{
LeftTop = radii.TopLeft + num;
TopLeft = radii.TopLeft + num2;
}
if (DoubleUtil.IsZero(radii.TopRight))
{
TopRight = (RightTop = 0.0);
}
else
{
TopRight = radii.TopRight + num2;
RightTop = radii.TopRight + num3;
}
if (DoubleUtil.IsZero(radii.BottomRight))
{
RightBottom = (BottomRight = 0.0);
}
else
{
RightBottom = radii.BottomRight + num3;
BottomRight = radii.BottomRight + num4;
}
if (DoubleUtil.IsZero(radii.BottomLeft))
{
BottomLeft = (LeftBottom = 0.0);
return;
}
BottomLeft = radii.BottomLeft + num4;
LeftBottom = radii.BottomLeft + num;
}
else
{
LeftTop = Math.Max(0.0, radii.TopLeft - num);
TopLeft = Math.Max(0.0, radii.TopLeft - num2);
TopRight = Math.Max(0.0, radii.TopRight - num2);
RightTop = Math.Max(0.0, radii.TopRight - num3);
RightBottom = Math.Max(0.0, radii.BottomRight - num3);
BottomRight = Math.Max(0.0, radii.BottomRight - num4);
BottomLeft = Math.Max(0.0, radii.BottomLeft - num4);
LeftBottom = Math.Max(0.0, radii.BottomLeft - num);
}
}
}
[CommonDependencyProperty]
public static readonly DependencyProperty BorderThicknessProperty;
public static readonly DependencyProperty PaddingProperty;
public static readonly DependencyProperty CornerRadiusProperty;
[CommonDependencyProperty]
public static readonly DependencyProperty BorderBrushProperty;
[CommonDependencyProperty]
public static readonly DependencyProperty BackgroundProperty;
private bool _u