3

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

理论上这应该有效,但它是白色的。

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

颜色转换器代码是

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
4

1 回答 1

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>

乍一看,a 似乎TemplateBinding是您需要的,但实际上您必须绑定到 a 的属性TemplatedParent才能在此处获得您想要的结果。

于 2013-10-04T14:37:18.693 回答