2011-07-30 19 views
5

वैल्यू = "{टेम्पलेट बाइंडिंग हेडर कॉलर}" के साथ सीमा पृष्ठभूमि सेट करना मैंने अपना नियंत्रण बनाया है, और मुझे आश्चर्य है कि क्या मैं एक सीमा को बांध सकता हूं। टेम्पलेट संपत्ति पर बैकग्राउंड।एक टेम्पलेट बाध्यकारी

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

मुझे पसंद है यह मेरे नियंत्रण पर एक संपत्ति होगी, और बाध्यकारी

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

mainpage एक टेम्पलेट के रूप में सेट करने में सक्षम हो: वर्तमान में मैं निम्नलिखित की तरह एक StaticResource साथ यह सेट कर रहा हूं .xaml

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

मेरी कक्षा:

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); } 
} 

यह दूसरा विकल्प काम नहीं करता है, क्या मुझे यह करने में सक्षम होना चाहिए? मुझे कोई त्रुटि नहीं मिलती है, यह सिर्फ मेरे द्वारा सेट किए गए रंग में नहीं बदलती है।

एंजेलडब्लूपीएफ द्वारा छोड़ी गई टिप्पणी ने अधिक कोड मांगे, नीचे चिपकाया, मैं नियंत्रण बनाने के शुरुआती चरणों में हूं, यह ध्यान रखना चाहता था, क्योंकि बहुत कुछ है जो मुझे अभी तक नहीं मिला है, एक टुकड़ा एक समय :)

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

क्या आप अधिक कोड पोस्ट कर सकते हैं? जहां आपके एक्सएएमएल में आप वास्तव में इस ColorAnimationUsingKeyFrames का उपयोग कर रहे हैं? चयन नियंत्रण के नियंत्रण टेम्पलेट के अंदर? यदि हां, तो टेम्पलेट बाइंडिंग काम करनी चाहिए! –

+0

हाय एंजेलडब्ल्यूपीएफ मैंने अपनी जेनेरिक.एक्सएएमएल फ़ाइल में कोड जोड़ा, और मेरे मूल प्रश्न के नीचे मेरे नियंत्रण वर्ग में कोड जोड़ा। किसी भी मदद के लिए धन्यवाद। हां, मैं अपने टेम्पलेट के अंदर ColorAnimationUsingKeyFrames का उपयोग कर रहा हूं, मैं शर्त लगा रहा हूं कि मुझे कुछ स्पष्ट याद आ रहा है। – Terco

+1

क्या आप देख सकते हैं कि टेम्पलेट बाइंडिंग आपके विजुअल स्टूडियो आउटपुट विंडो में विफल रही है या नहीं? जब एनीमेशन चलाने के लिए आपको बाध्यकारी त्रुटियां मिलती हैं? यदि आप हैं, तो आप सामान्य बाइंडिंग के साथ टेम्पलेट बाइंडिंग को बदलने का प्रयास कर सकते हैं और RelativeSource = {RelativeSource AncestorTpepe = {x: टाइप ctrl: Selection}} insetad का उपयोग कर सकते हैं ... –

उत्तर

6

मैं कस्टम निर्भरता गुणों पर TemplateBinding का उपयोग कर के मिश्रित परिणाम मिले है। इस वजह से, मैंने सापेक्ष स्रोत टेम्पलेटेड माता-पिता का उपयोग किया है जो हर स्थिति में काम करता प्रतीत होता है।

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

बहुत बहुत धन्यवाद, यह बहुत अच्छा काम किया! – Terco

संबंधित मुद्दे