Unfortunately, custom brushes are not supported in WPF (brush types are marked as "internal" and cannot be inherited), so creating a brush that is a mixture of two brushes that can be used from XAML, like a regular SolidColorBrush, is not possible.
MarkupExtension , XAML , SolidColorBrush ( ) , :
[MarkupExtensionReturnType(typeof(SolidColorBrush))]
public class MixedColorBrush : MarkupExtension, INotifyPropertyChanged
{
private SolidColorBrush foreground = Brushes.White;
private SolidColorBrush background = Brushes.Black;
public event PropertyChangedEventHandler PropertyChanged;
public SolidColorBrush Foreground
{
get
{
return this.foreground;
}
set
{
this.foreground = value;
this.NotifyPropertyChanged("Foreground");
}
}
public SolidColorBrush Background
{
get
{
return this.background;
}
set
{
this.background = value;
this.NotifyPropertyChanged("Background");
}
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (this.foreground != null && this.background != null)
{
return new SolidColorBrush(this.foreground.Color + this.background.Color);
}
return new SolidColorBrush();
}
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}">
, DynamicResource StaticResource . MarkupExtension DependencyObject, DependencyObjects; DependencyObjects, .