2013-10-04 4 views
2

Я создаю шаблон управления для флажка, все идет хорошо, но я хотел бы использовать градиент, привязанный к цвету переднего плана.Как связать с остановкой градиента WPF

Теоретически это должно работать, но оно выходит белым.

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

Опубликовать остаток вашего шаблона. – Nick

+0

как вы устанавливаете переднюю панель вашего флажка, на который вы применили этот шаблон. Также вы получаете какие-либо ошибки в окне вывода? – Nitin

+0

Просто установив Foreground = «Зеленый». Нет ошибок –

ответ

3

привязки, должен быть изменен незначительно:

<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, чтобы получить результат, который вы хотите здесь.

+0

Отличное спасибо! Кроме того, знаете ли вы, как я могу выровнять градиент с помощью Tick Lines? –

+0

@ Рихард Харрисон, это, вероятно, лучше спросить в отдельном вопросе. Я только посмотрел на часть кисти вашего шаблона здесь; даже не реализовал остальных. :) – Stewbob