DependencyProperty不在CustomControl上工作

时间:2015-11-20 14:50:06

标签: c# wpf xaml

我尝试使用一些自定义属性进行自定义控件,但是在尝试绑定到属性时,它似乎无法正常工作。

这是我的代码背后 UserProfile.cs

using System;
using System.Windows;
using System.Windows.Controls;

namespace Controls
{
    public class UserProfile : Control
    {
        static UserProfile()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(UserProfile),
                new FrameworkPropertyMetadata(typeof(UserProfile)));
        }

        #region PhotoSize DP
        /// <summary>
        /// Gets or sets the Label which is displayed next to the field
        /// </summary>
        public Double PhotoSize
        {
            get { return (Double)GetValue(PhotoSizeProperty); }
            set { SetValue(PhotoSizeProperty, value); }
        }
        /// <summary>
        /// Identified the Label dependency property
        /// </summary>
        public static readonly DependencyProperty PhotoSizeProperty =
            DependencyProperty.Register("PhotoSize", typeof(Double),
              typeof(UserProfile), null);
        #endregion
    }
}

XAML - Generic.xaml

<Style TargetType="{x:Type local:UserProfile}">
        <Setter Property="DataContext" Value="{x:Type local:UserProfile}" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="FontSize" Value="16" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:UserProfile}">
                    <Grid x:Name="circleGrid" Width="{Binding PhotoSize}">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="{Binding Path=ActualWidth, ElementName=circleGrid}" />
                            <RowDefinition />
                            <RowDefinition />
                        </Grid.RowDefinitions>
                        <Border x:Name="circleBorder"
                                Grid.Row="0"
                                CornerRadius="{Binding Path=ActualWidth, ElementName=circleGrid}"
                                Width="{Binding Path=ActualWidth, ElementName=circleGrid}"
                                Height="{Binding Path=ActualWidth, ElementName=circleGrid}"
                                BorderBrush="White"
                                BorderThickness="3">
                            <Border.Background>
                                <ImageBrush ImageSource="D:\Users\Martyn Ball\Pictures\11061728_10153409797331063_2946862347621203654_o.jpg" Stretch="UniformToFill" />
                            </Border.Background>
                        </Border>

                        <WrapPanel Grid.Row="1" HorizontalAlignment="Center">
                            <TextBlock x:Name="firstName"
                                       FontWeight="Bold"
                                       Foreground="{TemplateBinding Foreground}"
                                       Text="Martyn" />
                            <TextBlock Text=" "/>
                            <TextBlock x:Name="lastName"
                                       FontWeight="Normal"
                                       Foreground="{TemplateBinding Foreground}"
                                       Text="Ball" />
                        </WrapPanel>

                        <WrapPanel Grid.Row="2">
                            <WrapPanel Margin="5">
                                <TextBlock x:Name="imageDifference" Text="" />
                                <TextBlock Text="56" />
                            </WrapPanel>
                            <WrapPanel Margin="5">
                                <TextBlock x:Name="earningsDifference" Text="£" />
                                <TextBlock Text="£50.50" />
                            </WrapPanel>
                        </WrapPanel>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

错误:

  

BindingExpression路径错误:&#39; PhotoSize&#39;找不到物业   &#39;对象&#39; &#39;&#39; RuntimeType&#39; (的HashCode = 19976800)&#39 ;.   BindingExpression:路径= PhotoSize;的DataItem =&#39; RuntimeType&#39;   (的HashCode = 19976800);目标元素是网格&#39; (名称=&#39;&#39);目标   属性是宽度&#39; (键入&#39; Double&#39;)

1 个答案:

答案 0 :(得分:2)

您正在将DataContext设置为Typex:Type正在执行此操作)。您已经设置了TargetType。因此,请删除该行<Setter Property="DataContext" Value="{x:Type local:UserProfile}" />并将Binding更改为TemplateBinding,这样就可以了。 有关BindingTemplateBinding

之间差异的详细信息,请查看this问题