自定义控件上的 TemplateBinding to DependencyProperty 不起作用 [英] TemplateBinding to DependencyProperty on a custom control not working
问题描述
目前,我正在开发一个简单的自定义按钮,该按钮使用用户提供的图像作为按下状态和正常状态的背景.我有很多按钮,所以我决定编写一个自定义按钮并为按下状态和正常状态的图片实现两个属性.
Currently, I'm working on a simple custom button that uses user supplied images as a background for the pressed and normal states. I've a lot of buttons so I decided to write a custom button and implement two properties for the pressed and normal states' pictures.
这是我正在使用的代码
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);
}
}
}
我为这个按钮写的样式如下
I wrote the style for this button as follows
<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>
我尝试了一个简单的例子
I tried a simple example
<Controls:ThemeableButton Style="{StaticResource ThemeableButtonTemplate}" x:Name="btnDidntNeedIt" NormalContentBackgroundSource="{Binding Source={StaticResource DefaultTheme}, Path=DidntHaveButtonUnselected}"
PressedContentBackgroundSource="{Binding Source={StaticResource DefaultTheme}, Path=DidntHaveButtonSelected}"
/>
但图像没有显示,我尝试通过从样式中删除 TemplateBinding 并将其替换为图像文件的相对源,并且它工作正常.我只是不想为应用程序上的每个按钮创建自定义样式.任何可能的解决方法?
but the image is not showing, I tried by removing the TemplateBinding from the style and replaced it with the relative source to the image file and it worked fine. I just don't wanna create a customized style for each button on the app. Any possible workaround?
推荐答案
我以前遇到过这种情况,TemplateBinding 不适用于控件上的自定义依赖属性.请参阅以下相关问题:
I have encountered this before, TemplateBinding does not work for custom dependency properties on controls. See these related questions:
TemplateBinding 在某些情况下不起作用(使用 TranslateTransform 时)
我一直用它来代替:
{Binding MyProperty, RelativeSource={RelativeSource TemplatedParent}}
在语义上与 TemplateBinding 相同,并且还可以支持值转换器等......
It is semantically the same as TemplateBinding, and can also support value converters etc ...
这篇关于自定义控件上的 TemplateBinding to DependencyProperty 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!