0

Buttonこのような背景を持つ単純なユーザーコントロールがある場合...

<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
  <GradientStop Color="Red" Offset="1" />
  <GradientStop Color="Black" Offset="0" />
</LinearGradientBrush>

Redコントロールを使用する人が最初のもの (例: ) だけを変更し、グラデーションを に維持できるように、グラデーション ストップの 1 つを公開する方法はありBlackますか?

4

1 に答える 1

1

ユーザーが独自の XAML の一部としてグラデーションの停止色を設定することを意味していると思いますか? その場合は、 a を使用しDependencyPropertyて をそれにバインドできますGradientStop.Color

UserControl.cs で:

    public CoolControl()
    {
        InitalizeComponent();
        SetValue(ColorProperty, Colors.Red); // or any default color
    }

    public static DependencyProperty ColorProperty = DependencyProperty.Register("BackgroundColor", typeof(Color), typeof(CoolControl)); // replace CoolControl with the name of your UserControl

    public Color BackgroundColor
    {
        get
        {
            return (Color)GetValue(ColorProperty);
        }
        set
        {
            SetValue(ColorProperty, value);
        }
    }

UserControl.xaml で:

    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="{Binding BackgroundColor, RelativeSource={RelativeSource AncestorType={x:Type my:CoolControl}}}" Offset="1" /> <!-- replace my:CoolControl with your namespace declaration and UserControl name -->
        <GradientStop Color="Black" Offset="0" />
    </LinearGradientBrush>

コントロールの使用:

    <Grid>
        <my:CoolControl BackgroundColor="Blue" />
    </Grid>
于 2013-01-04T17:46:04.210 に答える