将rectange填充绑定到线性渐变画笔

时间:2011-10-27 17:32:32

标签: c# wpf

我有矩形,填充颜色由MultiBindingConverter从RGB滑块确定。

        <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>

现在我想将“选定”颜色设为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>

我尝试了ObjectDataProvider

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

其中ChosenColor是一个扩展方法:

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

但没有运气。怎么做?

2 个答案:

答案 0 :(得分:1)

您可以直接绑定到其他Rectangle的Fill.Color属性,如下所示 -

假设您的第一个矩形名称是 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>

答案 1 :(得分:1)

如果我理解正确,您的转换器会返回Color。您应该能够像指定SolidColorBrush一样指定GradientStop的颜色:

<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>