Martyn Ball
Martyn Ball

Reputation: 4895

DependencyProperty not working on CustomControl

I'm trying to make a custom control, with some custom properties, however when attempting to Bind to the property it doesn't seem to work.

Here is my Code Behind 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>

Error:

BindingExpression path error: 'PhotoSize' property not found on 'object' ''RuntimeType' (HashCode=19976800)'. BindingExpression:Path=PhotoSize; DataItem='RuntimeType' (HashCode=19976800); target element is 'Grid' (Name=''); target property is 'Width' (type 'Double')

Upvotes: 2

Views: 145

Answers (1)

wingerse
wingerse

Reputation: 3796

You are setting the DataContext to a Type (x:Type is doing it). You have set the TargetType already. So remove the line <Setter Property="DataContext" Value="{x:Type local:UserProfile}" /> and change the Bindings to TemplateBindings and it will work. Check this question for more info regarding the differences between Binding and TemplateBinding

Upvotes: 2

Related Questions