无法访问自定义控件模板中的自定义依赖项属性

时间:2014-08-24 23:05:57

标签: wpf

我为自定义按钮创建了两个新属性。但是,当我尝试在模板按钮上访问它时,VS说“该成员无法识别或无法访问”。

MyButton.cs

namespace myNameSpace
{
   public partial class MyButton : Button, IOperacoesComponentes
   {

       public static readonly DependencyProperty IconProperty =
           DependencyProperty.Register("Icon", typeof(string),
           typeof(MyButton), new FrameworkPropertyMetadata(null));
       public static readonly DependencyProperty TextProperty =
           DependencyProperty.Register("Text", typeof(string),
           typeof(MyButton), new FrameworkPropertyMetadata(null));

       public MyButton()
       {
           InitializeComponent();
       }

       public string Icon
       {
           get { return (string)GetValue(IconProperty); }
           set { SetValue(IconProperty, value); }
       }

       public string Text
       {
           get { return (string)GetValue(TextProperty); }
           set { SetValue(TextProperty, value); }
       }

  ...

  }

}

在MyButton.xaml文件中,当尝试访问它们时:

MyButton.xaml

<Button x:Class="myNameSpace.myButton"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d">

<Button.Resources>
    <Style TargetType="Button" x:Key="myButton">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" ClipToBounds="True">
                        <Border x:Name="border" CornerRadius="4" BorderBrush="transparent" BorderThickness="0">
                            <Border.Background>
                                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                    <GradientStop Color="#FF0088D9" Offset="0"/>
                                    <GradientStop Color="#FF007BC4" Offset="1"/>
                                </LinearGradientBrush>
                            </Border.Background>
                        </Border>
                        <TextBlock x:Name="content" Margin="{TemplateBinding Padding}" TextWrapping="Wrap" HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto" Height="Auto">
                            <Run Text="{TemplateBinding Icon}"/>
                            <Run Text="{TemplateBinding Text}"/>
                        </TextBlock>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Button.Resources>

两个新属性“Icon”和“Text”出现没有问题。但是当我尝试通过“TemplateBiding”访问它们时,VS会给出上述错误消息。

我做错了什么?在此先感谢!!

1 个答案:

答案 0 :(得分:4)

目标类型必须为MyButton,而非Button

<Button.Resources>
    <Style TargetType="MyButton" x:Key="myButton">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type MyButton}">