2011-12-28 2 views
27

현재 사용자 지정 이미지를 누른 상태 및 정상 상태의 배경으로 사용하는 간단한 사용자 지정 단추를 작업하고 있습니다. 버튼이 많아서 사용자 정의 버튼을 작성하고 눌려진 상태와 정상 상태의 그림에 대한 두 가지 특성을 구현하기로 결정했습니다. 여기 사용자 정의 컨트롤의 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'을 채 웁니다. 설계 시간 동안. 아마 2011 년 이래로 뭔가 새로운 것일까 요? 어쨌든 이제는 잘 작동합니다. (그러나 Freezeable이 아닌 것들에 애니메이션을 적용하는 것에주의하십시오. 당신은 여전히 ​​그것을 할 수 없습니다.) –

관련 문제