展示类
输入类
默认的Border不能剪切内部元素,自定义border支持内部剪切
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
namespace ZJClassTool.Views
{
public class ClippingBorder : Border
{
protected override void OnRender(DrawingContext dc)
{
OnApplyChildClip();
base.OnRender(dc);
}
public override UIElement Child
{
get
{
return base.Child;
}
set
{
if (Child != value)
{
if (Child != null)
{
// Restore original clipping
Child.SetValue(ClipProperty, _oldClip);
}
if (value != null)
{
_oldClip = value.ReadLocalValue(ClipProperty);
}
else
{
// If we dont set it to null we could leak a Geometry object
_oldClip = null;
}
base.Child = value;
}
}
}
protected virtual void OnApplyChildClip()
{
UIElement child = Child;
if (child != null)
{
var top = Math.Max(CornerRadius.TopLeft, CornerRadius.TopRight);
var bottom = Math.Max(CornerRadius.BottomLeft, CornerRadius.BottomRight);
var max = Math.Max(top, bottom);
var size = RenderSize;
var width = size.Width - (BorderThickness.Left + BorderThickness.Right);
var height = size.Height - (BorderThickness.Top + BorderThickness.Bottom);
Geometry result = new RectangleGeometry
(new Rect(0, 0, width, height), max, max);
double halfWidth = width / 2;
double halfHeight = height / 2;
if (CornerRadius.TopLeft == 0)
{
result = new CombinedGeometry(
GeometryCombineMode.Union,
result,
new RectangleGeometry(new Rect(0, 0, halfWidth, halfHeight))
);
}
if (CornerRadius.TopRight == 0)
{
result = new CombinedGeometry(GeometryCombineMode.Union, result, new RectangleGeometry
(new Rect(halfWidth, 0, halfWidth, halfHeight)));
}
if (CornerRadius.BottomLeft == 0)
{
result = new CombinedGeometry
(GeometryCombineMode.Union, result, new RectangleGeometry
(new Rect(0, halfHeight, halfWidth, halfHeight)));
}
if (CornerRadius.BottomRight == 0)
{
result = new CombinedGeometry
(GeometryCombineMode.Union, result, new RectangleGeometry
(new Rect(halfWidth, halfHeight, halfWidth, halfHeight)));
}
child.Clip = result;
}
}
public void Update() { OnApplyChildClip(); }
private RectangleGeometry _clipRect = new RectangleGeometry();
private object _oldClip;
}
}
ItemsControl、ListBox和ListView
ListView继承于ListBox,ListBox继承于ItemsControl,所以后面的组件拥有前面组件的一切特性。
相同点:
不同点:
ListView和DataGrid