deha
deha

Reputation: 815

Bind rectange fill to a lineargradientbrush

I have rectangle, which fill color is determined by MultiBindingConverter from RGB-sliders.

        <Rectangle.Fill>
            <SolidColorBrush>
                <SolidColorBrush.Color>
                    <MultiBinding Converter="{StaticResource RgbConverter}">
                        <Binding Path="Value" ElementName="RSlider" />      
                        <Binding Path="Value" ElementName="GSlider" />
                        <Binding Path="Value" ElementName="BSlider" />
                    </MultiBinding>
                </SolidColorBrush.Color>
            </SolidColorBrush>
        </Rectangle.Fill>

Now I want to make that "selected" color a GradientStop

        <Rectangle.Fill>
            <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                <GradientStop Offset="0" Color="Black" />
                <GradientStop Offset="1" Color="{here bind the color}" />
            </LinearGradientBrush>
        </Rectangle.Fill>

I tried through ObjectDataProvider

    <ObjectDataProvider ObjectInstance="{StaticResource ResourceKey=cColor}" MethodName="ChosenColor" x:Key="chColor" >
    </ObjectDataProvider>

where ChosenColor is an Extension method:

    public static Color ChosenColor(this Rectangle rect)
    {
        return ((SolidColorBrush)rect.Fill).Color;
    }

but no luck. How to do that?

Upvotes: 0

Views: 1356

Answers (2)

Rohit Vats
Rohit Vats

Reputation: 81253

You can bind directly to Fill.Color property of other Rectangle like this -

Assuming your first Rectangle name is rectangle1

<Rectangle.Fill>
  <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
     <GradientStop Offset="0" Color="Black" />
     <GradientStop Offset="1" Color="{Binding ElementName=rct, Path=Fill.Color}" />
   </LinearGradientBrush>
</Rectangle.Fill>

Upvotes: 1

Dan J
Dan J

Reputation: 16708

If I understand correctly, your converter returns a Color. You should be able to specify the GradientStop's color the same way that you specify the SolidColorBrush's:

<Rectangle.Fill>
    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
        <GradientStop Offset="0" Color="Black" />
        <GradientStop Offset="1">
            <GradientStop.Color>
                <MultiBinding Converter="{StaticResource RgbConverter}">
                    <Binding Path="Value" ElementName="RSlider" />      
                    <Binding Path="Value" ElementName="GSlider" />
                    <Binding Path="Value" ElementName="BSlider" />
                </MultiBinding>
            </GradientStop.Color>
        </GradientStop>
    </LinearGradientBrush>
</Rectangle.Fill>

Upvotes: 1

Related Questions