自定义控件中的TemplateBinding到DependencyProperty不起作用。

37

目前,我正在开发一个简单的自定义按钮,它使用用户提供的图像作为按下和正常状态的背景。我有很多按钮,因此我决定编写一个自定义按钮并实现两个属性,用于按下和正常状态的图片。

这是我使用的代码

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

我按照以下方式编写了这个按钮的样式

        <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}"
         />

但是图片没有显示,我尝试通过从样式中删除TemplateBinding并将其替换为图像文件的相对源,这样可以正常工作。我只是不想为应用程序上的每个按钮创建自定义样式。有什么可能的解决方法吗?

1个回答

91

这是对我也起作用的答案。我希望TemplateBinding能够正常工作,并消除一些神秘和武断的因素。 - TernaryTopiary
"...TemplateBinding在控件的自定义依赖属性上不起作用[。]" 对我来说它是有效的。在我的自定义控件的ControlTemplate中,<Rectangle Fill="{TemplateBinding FillBrush}" />使用了我的FillBrush自定义依赖属性填充了Rectangle,甚至在VS的属性表中显示该属性,并在设计时将其设置为任何我想要的值。也许这是自2011年以来的新功能?无论如何,现在它运行良好。(但要注意不能对非“Freezeable”对象进行动画处理。) - Stevens Miller
更具体地说(冒着SO机器人在评论中进行扩展讨论的风险),TemplateBinding不适用于Freezeable属性。据我所知,它会默默失败。您的解决方案当然适用于这些情况。 - Stevens Miller
1
这在 .NET 4.7.2 上似乎不起作用。他们关闭了这个漏洞吗? - CodeOtaku

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接