2011-07-30 17 views
5

Wartość = "{TemplateBinding HeaderColor}" Stworzyłem własną kontrolę i zastanawiam się, czy mogę powiązać Border.Background z właściwością szablonu. Obecnie jestem ustawienie go z StaticResource tak:Ustawianie tła obramowania z powiązaniem szablonu

<Color x:Key="ControlMouseOverColor">green</Color> 

<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="headerLayout"> 
    <EasingColorKeyFrame KeyTime="0:0:6" Value="{StaticResource ControlMouseOverColor}" /> 
</ColorAnimationUsingKeyFrames> 

Chciałbym to być nieruchomość na moją kontrolą, i być w stanie ustawić go jako szablon wiążącego

<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="headerLayout"> 
    <EasingColorKeyFrame KeyTime="0:0:6" Value="{TemplateBinding HeaderColor}" /> 
</ColorAnimationUsingKeyFrames> 

mainpage .xaml

<ctrl:Selection Grid.Column="0" HeaderColor="Red" HeaderText="Header Text" /> 

moja klasa:

public static readonly DependencyProperty HeaderColorProperty = 
     DependencyProperty.Register("HeaderColor", typeof(System.Windows.Media.Color), typeof(Selection), new PropertyMetadata(System.Windows.Media.Colors.Red)); 

public System.Windows.Media.Color HeaderColor { 
    get { return (System.Windows.Media.Color)GetValue(HeaderColorProperty); } 
    set { SetValue(HeaderColorProperty, value); } 
} 

Ta druga opcja nie działa, czy powinienem móc to zrobić? Nie dostaję błędu, po prostu nie zmienia się on na ustawiony przeze mnie kolor.

Komentarz pozostawiony przez AngelWPF poprosił o więcej kodu, wkleiłem poniżej, Jestem na początkowych etapach nauki tworzenia kontroli, chciałbym to zauważyć, ponieważ jest dużo, czego jeszcze nie mam, jeden kawałek na czas :)

generic.xaml

<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:controls="clr-namespace:SelectionControl.Library" 
xmlns:ctrl="clr-namespace:SelectionControl.Library;assembly=SelectionControl"> 

<LinearGradientBrush x:Key="HeaderBackground" EndPoint="0.5,1" StartPoint="0.5,0"> 
    <GradientStop Color="Black" Offset="0" /> 
    <GradientStop Color="Gray" Offset="1" /> 
</LinearGradientBrush> 

<Color x:Key="ControlMouseEnterColor">aliceblue</Color> 
<Color x:Key="ControlMouseLeaveColor">Gray</Color> 
<Color x:Key="ControlLeftMouseUpColor">Red</Color> 

<Style TargetType="ctrl:Selection"> 
    <Setter Property="Width" Value="Auto" /> 
    <Setter Property="Height" Value="Auto" /> 
    <Setter Property="FontSize" Value="12" /> 
    <Setter Property="FontWeight" Value="Bold" /> 
    <Setter Property="Foreground" Value="AliceBlue" /> 
    <Setter Property="Margin" Value="2,2,2,2" /> 

    <Setter Property="Background" Value="{StaticResource ResourceKey=HeaderBackground}" /> 

    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ctrl:Selection"> 
       <Grid x:Name="RootElement" Margin="{TemplateBinding Margin}"> 
        <!-- Visual States --> 
        <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
          <VisualState x:Name="MouseEnter"> 
           <Storyboard> 
            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="headerLayout"> 
             <EasingColorKeyFrame KeyTime="0:0:.5" Value="{TemplateBinding HeaderColor}" /> 
            </ColorAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="MouseLeave"> 
           <Storyboard> 
            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="headerLayout"> 
             <EasingColorKeyFrame KeyTime="0:0:1" Value="{StaticResource ControlMouseLeaveColor}" /> 
            </ColorAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="MouseLeftUp"> 
           <Storyboard> 
            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="headerLayout"> 
             <EasingColorKeyFrame KeyTime="0:0:1" Value="{StaticResource ControlLeftMouseUpColor}" /> 
            </ColorAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
        <!-- End Visual States--> 

        <Grid.RowDefinitions> 
         <RowDefinition Height="Auto"/> 
         <RowDefinition Height="*"/> 
        </Grid.RowDefinitions> 
        <!-- Header --> 
        <Border x:Name="headerLayout" Background="{TemplateBinding Background}" Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CornerRadius="2,2,2,2" BorderBrush="Black" BorderThickness="1"> 
         <StackPanel> 
          <ToggleButton ></ToggleButton> 
          <TextBlock Foreground="{TemplateBinding Foreground}" Text="{TemplateBinding HeaderText}" FontWeight="{TemplateBinding FontWeight}" FontSize="{TemplateBinding FontSize}" HorizontalAlignment="Center" VerticalAlignment="Center" /> 
         </StackPanel> 
        </Border> 
        <!-- Body Content --> 
        <ContentPresenter Grid.Row="1" Content="{TemplateBinding Content}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" /> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
</ResourceDictionary> 

Selection.cs

using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Input; 

namespace SelectionControl.Library { 
    [TemplateVisualState(Name = Selection.MouseEnterStateName, GroupName = Selection.CommonStatesGroupName)] 
    [TemplateVisualState(Name = Selection.MouseLeaveStateName, GroupName = Selection.CommonStatesGroupName)] 
    [TemplateVisualState(Name = Selection.MouseLeftUpStateName, GroupName = Selection.CommonStatesGroupName)] 

public class Selection : ContentControl { 
    public const string CommonStatesGroupName = "CommonStates"; 
    public const string MouseEnterStateName = "MouseEnter"; 
    public const string MouseLeaveStateName = "MouseLeave"; 
    public const string MouseLeftUpStateName = "MouseLeftUp"; 

    public Selection() { 
     this.DefaultStyleKey = typeof(Selection); 

     this.MouseEnter += new MouseEventHandler(OnMouseEnter); 
     this.MouseLeave += new MouseEventHandler(OnMouseLeave); 
     this.MouseLeftButtonUp += new MouseButtonEventHandler(OnMouseLeftButtonUp); 
    } 

    void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { 
     this.GoToState(Selection.MouseLeftUpStateName, true); 
    } 

    void OnMouseLeave(object sender, MouseEventArgs e) { 
     this.GoToState(Selection.MouseLeaveStateName, true); 
    } 

    void OnMouseEnter(object sender, MouseEventArgs e) { 
     this.GoToState(Selection.MouseEnterStateName, true); 
    } 
    private void GoToState(string stateName, bool useTransitions) { 
     VisualStateManager.GoToState(this, stateName, useTransitions); 
    } 

    public static readonly DependencyProperty HeaderTextProperty = 
     DependencyProperty.Register("HeaderText", typeof(string), typeof(Selection), new PropertyMetadata("")); 

    public string HeaderText { 
     get { return (string)GetValue(HeaderTextProperty); } 
     set { SetValue(HeaderTextProperty, value); } 
    } 

    public static readonly DependencyProperty HeaderColorProperty = 
     DependencyProperty.Register("HeaderColor", typeof(System.Windows.Media.Color), typeof(Selection), new PropertyMetadata(System.Windows.Media.Colors.Red)); 

    public System.Windows.Media.Color HeaderColor { 
     get { return (System.Windows.Media.Color)GetValue(HeaderColorProperty); } 
     set { SetValue(HeaderColorProperty, value); } 
    } 
}} 
+0

Czy możesz dodać więcej kodu? gdzie w twoim XAMLu używasz właśnie ColorAnimationUsingKeyFrames? Wewnątrz szablonu kontrolnego kontrolki wyboru? Jeśli tak, TemplateBinding powinien działać! –

+0

Cześć AngelWPF Dodałem kod do pliku generic.xaml, a kod w mojej klasie kontrolnej pod moim pierwotnym pytaniem. Dzięki za pomoc. Tak, używam ColorAnimationUsingKeyFrames w moim szablonie, założę się, że brakuje mi czegoś oczywistego. – Terco

+1

Czy można zaobserwować, czy powiązanie szablonu nie działa w oknie wyjściowym Visual Studio? Czy otrzymujesz błędy wiązania podczas animacji? Jeśli tak, to możesz spróbować zastąpić wiązanie szablonu zwykłym wiązaniem i użyć RelativeSource = {RelativeSource AncestorType = {x: Typ ctrl: Selection}} insetad ... –

Odpowiedz

6

miałem mieszane rezultaty używając TemplateBinding na właściwości niestandardowych zależności. Z tego powodu użyłem RelativeSource TemplatedParent, który wydaje się działać w każdej sytuacji.

<EasingColorKeyFrame KeyTime="0:0:.5" Value="{Binding HeaderColor, RelativeSource={RelativeSource TemplatedParent}}" /> 
+0

Dziękuję bardzo, działało świetnie! – Terco

Powiązane problemy