C# WPF组合多画笔
是否可以将多个C# WPF组合多画笔,c#,wpf,xaml,drawingbrush,C#,Wpf,Xaml,Drawingbrush,是否可以将多个绘图画笔组合成一个,或者,是否有方法将多个画笔分配给我的网格 <Grid Name="gridContainer" Background="{StaticResource GridTile, RectangeGridTile}"> 显然,上面的代码不起作用。源代码: <DrawingBrush x:Key="GridTile" Viewport="0,0,4,16" ViewportUnits="Absolute" TileMode="Tile">
绘图画笔
组合成一个,或者,是否有方法将多个画笔
分配给我的网格
<Grid Name="gridContainer" Background="{StaticResource GridTile, RectangeGridTile}">
显然,上面的代码不起作用。源代码:
<DrawingBrush x:Key="GridTile" Viewport="0,0,4,16" ViewportUnits="Absolute" TileMode="Tile">
<DrawingBrush.Drawing>
<DrawingGroup>
<GeometryDrawing Geometry="M0,0 L1,0 1,0.05, 0,0.05Z" Brush="Black" />
<GeometryDrawing Geometry="M0,0 L0,1 0.1,1, 0.1,0Z" Brush="Black" />
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
<DrawingBrush x:Key="RectangeGridTile" Viewport="0,0,120,48" ViewportUnits="Absolute" TileMode="Tile">
<DrawingBrush.Drawing >
<DrawingGroup>
<GeometryDrawing Geometry="M0,0 L1,0 1,0.05, 0,0.05Z" Brush="Black" />
<GeometryDrawing Geometry="M0,0 L0,1 0.1,1, 0.1,0Z" Brush="Black" />
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
WPF中不支持自定义笔刷(笔刷类型为内部
,无法从中继承),因此无法创建笔刷
您可以使用MarkupExtension来模拟自定义画笔的行为,这允许您使用XAML语法并提供自定义值,这允许我们使用内置的SolidColorBrush设置为值,例如,两种混合颜色:
/// <summary>
/// Markup extension to mix two SolidColorBrushes together to produce a new SolidColorBrush.
/// </summary>
[MarkupExtensionReturnType(typeof(SolidColorBrush))]
public class MixedColorBrush : MarkupExtension, INotifyPropertyChanged
{
/// <summary>
/// The foreground mix color; defaults to white.
/// If not changed, the result will always be white.
/// </summary>
private SolidColorBrush foreground = Brushes.White;
/// <summary>
/// The background mix color; defaults to black.
/// If not set, the result will be the foreground color.
/// </summary>
private SolidColorBrush background = Brushes.Black;
/// <summary>
/// PropertyChanged event for WPF binding.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Gets or sets the foreground mix color.
/// </summary>
public SolidColorBrush Foreground
{
get
{
return this.foreground;
}
set
{
this.foreground = value;
this.NotifyPropertyChanged("Foreground");
}
}
/// <summary>
/// Gets or sets the background mix color.
/// </summary>
public SolidColorBrush Background
{
get
{
return this.background;
}
set
{
this.background = value;
this.NotifyPropertyChanged("Background");
}
}
/// <summary>
/// Returns a SolidColorBrush that is set as the value of the
/// target property for this markup extension.
/// </summary>
/// <param name="serviceProvider">Object that can provide services for the markup extension.</param>
/// <returns>The object value to set on the property where the extension is applied.</returns>
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (this.foreground != null && this.background != null)
{
// Create a new brush as a composite of the old ones
// This does simple non-perceptual additive color, e.g
// blue + red = magenta, but you can swap in a different
// algorithm to do subtractive color (red + yellow = orange)
return new SolidColorBrush(this.foreground.Color + this.background.Color);
}
// If either of the brushes was set to null, return an empty (white) brush.
return new SolidColorBrush();
}
/// <summary>
/// Raise the property changed event.
/// </summary>
/// <param name="propertyName">Name of the property which has changed.</param>
protected void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
//
///标记扩展,将两个SolidColorBrush混合在一起以生成新的SolidColorBrush。
///
[MarkupExtensionReturnType(typeof(SolidColorBrush))]
公共类MixedColorBrush:MarkupExtension,INotifyPropertyChanged
{
///
///前景混合颜色;默认为白色。
///如果不更改,结果将始终为白色。
///
私有SolidColorBrush前景=笔刷。白色;
///
///背景混合颜色;默认为黑色。
///如果未设置,结果将为前景色。
///
私有SolidColorBrush背景=笔刷。黑色;
///
///WPF绑定的PropertyChanged事件。
///
公共事件属性更改事件处理程序属性更改;
///
///获取或设置前景混合颜色。
///
公共色刷前景
{
得到
{
将此文件返回前台;
}
设置
{
这个。前景=价值;
本条。NotifyPropertyChanged(“前台”);
}
}
///
///获取或设置背景混合颜色。
///
公共色刷背景
{
得到
{
返回此.background;
}
设置
{
这个背景=值;
本条。NotifyPropertyChanged(“背景”);
}
}
///
///返回一个SolidColorBrush,该笔刷被设置为
///此标记扩展的目标属性。
///
///对象,该对象可以为标记扩展提供服务。
///要在应用扩展的属性上设置的对象值。
公共覆盖对象ProviderValue(IServiceProvider服务提供程序)
{
if(this.foreground!=null&&this.background!=null)
{
//创建一个新笔刷作为旧笔刷的组合
//这不需要简单的非感知加色,例如
//蓝色+红色=洋红色,但您可以换成其他颜色
//进行减色的算法(红色+黄色=橙色)
返回新的SolidColorBrush(this.foreground.Color+this.background.Color);
}
//如果其中一个笔刷设置为null,则返回空(白色)笔刷。
返回新的SolidColorBrush();
}
///
///引发属性已更改事件。
///
///已更改的属性的名称。
受保护的void NotifyPropertyChanged(字符串propertyName)
{
if(this.PropertyChanged!=null)
{
this.PropertyChanged(this,newpropertychangedventargs(propertyName));
}
}
}
然后可以从XAML使用,就像使用普通笔刷一样:
<Grid>
<Grid.Background>
<local:MixedColorBrush Foreground="Blue" Background="Red"/>
</Grid.Background>
</Grid>
或者使用标记扩展语法:
<Grid Background="{local:MixedColorBrush Foreground=Blue, Background=Red}">
注意:
不能使用DynamicResource
或StaticResource
引用将值绑定到应用程序中的其他资源MarkupExtension不是DependencyObject,资源绑定只对DependencyObject起作用;内置画笔是DependencyObjects,这就是为什么绑定可以与传统画笔一起工作。您解决了这个问题吗?