WPF结合多个DrawingBrush

WPF combining multiple DrawingBrush

是否可以将多个 DrawingBrushes 合并为一个,或者,是否可以将多个 Brushes 分配给我的 Grid

<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 不支持自定义画笔(画笔类型 internal 且无法继承),因此无法创建画笔。

您可以使用 MarkupExtension 来模拟自定义画笔的行为,这允许您使用 XAML 语法并提供自定义值,这允许我们使用内置的 SolidColorBrush 设置为 valueof ,比方说,两种混合颜色:

/// <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));
        }
    }
}

然后可以像使用普通画笔一样从 XAML 使用它:

<Grid>
    <Grid.Background>
        <local:MixedColorBrush Foreground="Blue" Background="Red"/>
    </Grid.Background>
</Grid>

或者使用标记扩展语法:

<Grid Background="{local:MixedColorBrush Foreground=Blue, Background=Red}">

注:

您不能使用 DynamicResourceStaticResource 引用将值绑定到应用程序中的其他资源。 MarkupExtension 不是 DependencyObject,资源绑定仅适用于 DependencyObjects;内置画笔是 DependencyObjects,这就是绑定与传统画笔一起工作的原因。