如何绑定到渐变停止WPF

时间:2013-10-04 13:24:22

标签: wpf binding colors templatebinding

我正在为复选框创建一个控件模板,一切顺利,但我想使用绑定到Foreground颜色的渐变。

从理论上讲,这应该可行,但它会变成白色。

<LinearGradientBrush x:Key="Shine" StartPoint="0,0" EndPoint="1,1">
    <GradientStopCollection>
        <GradientStop Offset="0" Color="{TemplateBinding Property=Foreground, Converter={StaticResource Color}}"/>
        <GradientStop Offset="0.5" Color="White"/>
        <GradientStop Offset="1" Color="{TemplateBinding Property=Foreground, Converter={StaticResource Color}}"/>
    </GradientStopCollection>
</LinearGradientBrush>

ColorConverter代码是

Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
    Dim S As SolidColorBrush = value
    Return S.Color
End Function

Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
    Return New SolidColorBrush(value)
End Function

我猜我错过了什么,但无法理解。

**这是完整的资源字典:

<ResourceDictionary>
        <local:Reducer x:Key="Reducer"/>
        <local:StrokeConverter x:Key="Stroke"/>
        <local:VisibilityConverter x:Key="Visible"/>
        <local:ColorConverter x:Key="Color"/>
        <LinearGradientBrush x:Key="Green" StartPoint="0,0" EndPoint="1,1">
            <GradientStopCollection>
                <GradientStop Offset="0" Color="Green"/>
                <GradientStop Offset="0.5" Color="White"/>
                <GradientStop Offset="1" Color="Green"/>
            </GradientStopCollection>
        </LinearGradientBrush>
        <ControlTemplate x:Key="Chk" TargetType="CheckBox">
            <ControlTemplate.Resources>
                <LinearGradientBrush x:Key="Shine" StartPoint="0,0" EndPoint="1,1">
                    <GradientStopCollection>
                        <GradientStop Offset="0" Color="{TemplateBinding Property=Foreground}"/>
                        <GradientStop Offset="0.5" Color="White"/>
                        <GradientStop Offset="1" Color="{TemplateBinding Property=Foreground, Converter={StaticResource Color}}"/>
                    </GradientStopCollection>
                </LinearGradientBrush>
            </ControlTemplate.Resources>
            <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="{Binding RelativeSource={RelativeSource Mode=Self}, Path=ActualHeight, Converter={StaticResource Stroke}, ConverterParameter=10}"
                    Height="{TemplateBinding Height}" Width="{TemplateBinding Height}">
                <Grid Visibility="{TemplateBinding Converter={StaticResource Visible}, Property=IsChecked}" >
                    <Line Stroke="{StaticResource Shine}" StrokeThickness="{Binding RelativeSource={RelativeSource AncestorType=Grid},Path=ActualHeight,Converter={StaticResource Stroke}, ConverterParameter=6}" StrokeStartLineCap="Round" StrokeEndLineCap="Round"
                        X1="{Binding RelativeSource={RelativeSource AncestorType=Grid},Path=ActualHeight,Converter={StaticResource Reducer}, ConverterParameter=.15}"
                        X2="{Binding RelativeSource={RelativeSource AncestorType=Grid},Path=ActualHeight,Converter={StaticResource Reducer}, ConverterParameter=.35}"
                        Y1="{Binding RelativeSource={RelativeSource AncestorType=Grid},Path=ActualHeight,Converter={StaticResource Reducer}, ConverterParameter=.6}"
                        Y2="{Binding RelativeSource={RelativeSource AncestorType=Grid},Path=ActualHeight,Converter={StaticResource Reducer}, ConverterParameter=.85}"
                        />
                    <Line Stroke="{StaticResource Shine}" StrokeThickness="{Binding RelativeSource={RelativeSource AncestorType=Grid},Path=ActualHeight,Converter={StaticResource Stroke},ConverterParameter=6}" StrokeStartLineCap="Round" StrokeEndLineCap="Round"
                        X1="{Binding RelativeSource={RelativeSource AncestorType=Grid},Path=ActualHeight, Converter={StaticResource Reducer}, ConverterParameter=.35}"
                        X2="{Binding RelativeSource={RelativeSource AncestorType=Grid},Path=ActualHeight, Converter={StaticResource Reducer}, ConverterParameter=.85}"
                        Y1="{Binding RelativeSource={RelativeSource AncestorType=Grid},Path=ActualHeight,Converter={StaticResource Reducer}, ConverterParameter=.85}"
                        Y2="{Binding RelativeSource={RelativeSource AncestorType=Grid},Path=ActualHeight,Converter={StaticResource Reducer}, ConverterParameter=.15}"
                        />
                </Grid>
            </Border>
        </ControlTemplate>
    </ResourceDictionary>

转换器:

Public Class Reducer
Implements IValueConverter

Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
    Return value * parameter
End Function

Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
    Return value
End Function
End Class

Public Class StrokeConverter
Implements IValueConverter

Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
    Return value / parameter
End Function

Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
    Return value
End Function
End Class

Public Class VisibilityConverter
Implements IValueConverter

Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
    Dim V As Visibility = Visibility.Visible
    If value = False Then V = Visibility.Collapsed
    Return V
End Function

Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
    Return CType(value, Visibility) = Visibility.Visible
End Function
End Class

Public Class ColorConverter
Implements IValueConverter

Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
    Dim S As SolidColorBrush = value
    Return S.Color
End Function

Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
    Return New SolidColorBrush(value)
End Function
End Class

1 个答案:

答案 0 :(得分:4)

您的绑定需要稍加修改:

<LinearGradientBrush x:Key="Shine" StartPoint="0,0" EndPoint="1,1">
  <GradientStopCollection>
    <GradientStop Offset="0" Color="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource Color}}"/>
    <GradientStop Offset="0.5" Color="White"/>
    <GradientStop Offset="1" Color="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource Color}}"/>
  </GradientStopCollection>
</LinearGradientBrush>

乍一看,似乎合理的是TemplateBinding是你需要的,但实际上你必须绑定到TemplatedParent的属性才能达到你想要的结果。