2011-12-28 11 views
27

現在、私は、ユーザーが提供するイメージを使用して、押された状態と通常の状態の背景として使用する単純なカスタムボタンを作成しています。私はたくさんのボタンを持っていましたので、私はカスタムボタンを書いて、押された状態と正常な状態の写真の2つのプロパティを実装することに決めました。ここでカスタムコントロールのDependencyPropertyへのTemplateBindingが機能しない

 <Style x:Key="ThemeableButtonTemplate" TargetType="MJbox_UIComponents_Controls:ThemeableButton"> 
     <Setter Property="Background" Value="Transparent"/> 
     <Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/> 
     <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/> 
     <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/> 
     <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiBold}"/> 
     <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}"/> 
     <Setter Property="Padding" Value="0"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="MJbox_UIComponents_Controls:ThemeableButton"> 
        <Grid> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualState x:Name="Normal"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Source" Storyboard.TargetName="ButtonBackground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{TemplateBinding NormalContentBackgroundSource}"> 
              </DiscreteObjectKeyFrame> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Pressed"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Source" Storyboard.TargetName="ButtonBackground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{TemplateBinding PressedContentBackgroundSource}"> 
              </DiscreteObjectKeyFrame> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <Border BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="0"> 
          <Image x:Name="ButtonBackground" Stretch="None" Source="{TemplateBinding NormalContentBackgroundSource}"/> 
         </Border>  
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

を次のように私は私が

<Controls:ThemeableButton Style="{StaticResource ThemeableButtonTemplate}" x:Name="btnDidntNeedIt" NormalContentBackgroundSource="{Binding Source={StaticResource DefaultTheme}, Path=DidntHaveButtonUnselected}" 
            PressedContentBackgroundSource="{Binding Source={StaticResource DefaultTheme}, Path=DidntHaveButtonSelected}" 
     /> 

が、画像の簡単な例を試してみました。このボタンのスタイルを書いた私は

public partial class ThemeableButton : Button 
{ 
    public ThemeableButton() 
    { 
     InitializeComponent(); 
    } 


    public static readonly DependencyProperty PressedContentBackgroundSourceProperty = DependencyProperty.Register(
        "PressedContentBackgroundSource", typeof(ImageSource), typeof(ThemeableButton), null); 
    public ImageSource PressedContentBackgroundSource 
    { 
     get { return (ImageSource)GetValue(PressedContentBackgroundSourceProperty); } 
     set 
     { 
      (value as BitmapImage).CreateOptions = BitmapCreateOptions.BackgroundCreation; 
      SetValue(PressedContentBackgroundSourceProperty, value); 
     } 
    } 


    public static readonly DependencyProperty NormalContentBackgroundSourceProperty = 
     DependencyProperty.Register("NormalContentBackgroundSource", typeof(ImageSource), typeof(ThemeableButton), null); 

    public ImageSource NormalContentBackgroundSource 
    { 
     get { return (ImageSource)GetValue(NormalContentBackgroundSourceProperty); } 
     set 
     { 
      (value as BitmapImage).CreateOptions = BitmapCreateOptions.BackgroundCreation; 
      SetValue(NormalContentBackgroundSourceProperty, value); 
     } 
    } 
} 

を使用しているコードですが表示されていない、私はスタイルからTemplateBindingを削除して、画像の相対的なソースに置き換えてみましたファイルとそれは正常に働いた。私はちょうどアプリの各ボタンのためのカスタマイズされたスタイルを作成したくありません。可能な回避策はありますか?

答えて

71

これまでに、コントロールのカスタム依存関係プロパティでTemplateBindingが機能しません。これらの関連の質問を参照してください。

issues with template binding and binding of custom component

TemplateBinding does not work in certain cases(when using TranslateTransform)

私はいつもこの代わりに使用しています

{Binding MyProperty, RelativeSource={RelativeSource TemplatedParent}} 

それは意味的にTemplateBindingのと同じであり、また、値コンバータなどをサポートすることができます。..

+0

ありがとうございました。私は仕事をしてくれてありがとうございます。 – Waleed

+0

これはうまくいきました僕にも。私はTemplateBindingがちょうど働いていて、謎と恣意性のいくつかが解消されたことを願っています。 – TernaryTopiary

+0

"... TemplateBindingは、コントロールのカスタム依存プロパティーでは機能しません。"私のカスタムコントロールの 'ControlTemplate'の' 'は私の' FillBrush'カスタム依存プロパティで 'Rectangle'を塗りつぶし、VSプロパティシートのそのプロパティを表示して、それは設計時にです。おそらく2011年以来の新しいことでしょうか?とにかく、今は正常に動作します。 (しかし、 'Freezeable'ではないものにアニメートすることに注意してください。まだそれを行うことはできません。) –

関連する問題