【问题标题】:DependencyProperty not working on CustomControlDependencyProperty 不适用于 CustomControl
【发布时间】:2015-11-20 23:54:20
【问题描述】:

我正在尝试制作一个带有一些自定义属性的自定义控件,但是在尝试绑定到该属性时它似乎不起作用。

这是我在 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 路径错误:“PhotoSize”属性未在 '对象' ''RuntimeType' (HashCode=19976800)'。 绑定表达式:路径=照片大小; DataItem='运行时类型' (哈希码=19976800);目标元素是'网格'(名称='');目标 属性是“宽度”(类型“双”)

【问题讨论】:

  • “它不起作用” - 怎么样?有什么错误吗?
  • @EmpereurAiman,废话,我的剪贴板中有错误,但忘了把它放进去!大声笑现在会添加它

标签: c# wpf xaml


【解决方案1】:

您正在将 DataContext 设置为 Typex:Type 正在这样做)。您已经设置了TargetType。所以删除行&lt;Setter Property="DataContext" Value="{x:Type local:UserProfile}" /&gt;并将Bindings更改为TemplateBindings,它会起作用。 查看this 问题以获取有关BindingTemplateBinding 之间差异的更多信息

【讨论】:

  • 完成此操作,仍然无法正常工作,但没有错误了。
  • 我对控件的使用:&lt;Controls:UserProfile PhotoSize="50" /&gt;
  • 哦,尝试在模板中使用 TemplateBinding。
  • &lt;Grid x:Name="circleGrid" Width="{Binding PhotoSize}"&gt;更改为&lt;Grid x:Name="circleGrid" Width="{TemplateBinding PhotoSize}"&gt;
  • 那是因为您正在为 Converter 分配一个字符串。将转换器放入资源并使用 {StaticResource} 扩展。就像在资源中一样。:&lt;namespace:CalculateBorder x:key="converter"... 然后在您的绑定中,Converter={StaticResource converter}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-07
  • 1970-01-01
  • 2011-06-27
  • 1970-01-01
  • 2012-09-01
  • 2021-11-08
相关资源
最近更新 更多