2本の無地ブラシを組み合わせた一種の「無地ブラシ」を作成する方法はありますか?
バックカラーについては、他のブラシのDynamicReferenceを使用できるようにしたいと思います。一方、他の色(前面)は不透明な静的な色である可能性があります。
これが本当に意味をなさない場合は、遠慮なく説明を求めてください!
私は同じ問題に遭遇しました。私は通常、基本的なダーク、基本的なライトにそれぞれ 1 つの xaml を使用し、次に各カラー アクセント (青、赤など) に 1 つ使用します。アクセントはわずかにルックスルーで、暗い背景で暗いテーマを選択すると暗くなります。
アプリでコントラストを高めるためにセカンダリ アクセント カラーを使用してテーマを作成する場合 (たとえば、明るいテーマが選択されている場合は灰色、暗いテーマが選択されている場合はアクセント カラー)、2 つの色からブラシを作成する必要がありました。各色の暗いテーマと明るいテーマ。
これが私が使用するものです:
<DrawingBrush x:Key="SecondaryAccentColorBrush" Viewport="0,0,1,1" TileMode="Tile">
<DrawingBrush.Drawing>
<DrawingGroup>
<GeometryDrawing>
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,1,1" />
</GeometryDrawing.Geometry>
<GeometryDrawing.Brush>
<SolidColorBrush Color="{DynamicResource AccentColor}"/>
</GeometryDrawing.Brush>
</GeometryDrawing>
<GeometryDrawing>
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,1,1" />
</GeometryDrawing.Geometry>
<GeometryDrawing.Brush>
<SolidColorBrush Color="{DynamicResource Gray10}"/>
</GeometryDrawing.Brush>
</GeometryDrawing>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
テーマを切り替えると、「Gray10」のアルファが 00 と FF の間で切り替わり、ブラシはグレーまたはアクセント カラーのいずれかを表示します。
残念ながら、カスタム ブラシは WPF ではサポートされていません (ブラシ タイプは「内部」とマークされており、継承できません)。そのため、通常の SolidColorBrush のように XAML から使用できる 2 つのブラシを組み合わせたブラシを作成することはできません。
回避策として、MarkupExtension を使用してカスタム ブラシの動作をシミュレートすることができます。これにより、XAML 構文を使用してカスタム値を提供できるようになり、組み込みの SolidColorBrush (カスタム ブラシは不要) を2 色を混ぜたときに得られる値:
/// <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}">
このアプローチの欠点は、DynamicResource または StaticResource 参照を使用して、アプリケーション内の他のリソースに値をバインドできないことです。MarkupExtension は DependencyObject ではなく、リソース バインディングは DependencyObject でのみ機能します。組み込みのブラシは DependencyObject であるため、バインディングは従来のブラシで機能します。
前景ブラシと背景ブラシから色を取得して混合し、結果の色から新しいブラシを作成します。
C# での例:
Color foreground = foregroundBrush.Color;
Color background = backgroundBrush.Color;
int opacity = 25;
int r = (opacity * (foreground.R - background.R) / 100) + background.R;
int g = (opacity * (foreground.G - background.G) / 100) + background.G;
int b = (opacity * (foreground.B - background.B) / 100) + background.B;
SolidColorBrush mixedBrush = new SolidColorBrush(Color.FromArgb(r, g, b));
それを行う簡単な方法の 1 つ (ただし、おそらく最適化されていません) は、2 つの色のLinearGradientBrushを繰り返しモードで作成し、Endpoint を開始点に等しくします。
<LinearGradientBrush SpreadMethod="Repeat" EndPoint="0,0">
<GradientStop Color="Red" Offset="0" />
<GradientStop Color="Yellow" Offset="1" />
</LinearGradientBrush>
これにより、オレンジ色のブラシが得られます。