C# DependencyProperty不在CustomControl上工作
我试图创建一个带有一些自定义属性的自定义控件,但是当试图绑定到该属性时,它似乎不起作用 这是我的UserProfile.cs背后的代码C# DependencyProperty不在CustomControl上工作,c#,wpf,xaml,C#,Wpf,Xaml,我试图创建一个带有一些自定义属性的自定义控件,但是当试图绑定到该属性时,它似乎不起作用 这是我的UserProfile.cs背后的代码 using System; using System.Windows; using System.Windows.Controls; namespace Controls { public class UserProfile : Control { static UserProfile() {
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
}
}
使用系统;
使用System.Windows;
使用System.Windows.Controls;
命名空间控件
{
公共类UserProfile:控件
{
静态用户配置文件()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(UserProfile)),
新的FrameworkPropertyMetadata(typeof(UserProfile));
}
#区域光照DP
///
///获取或设置显示在字段旁边的标签
///
公共双照片
{
获取{return(Double)GetValue(photozizeproperty);}
set{SetValue(PhotoSizeProperty,value);}
}
///
///已标识标签依赖项属性
///
公共静态只读从属属性PhotoSizeProperty=
DependencyProperty.寄存器(“PhotoSize”,类型为(双),
typeof(UserProfile),null);
#端区
}
}
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)'。
BindingExpression:Path=PhotoSize;DataItem='RuntimeType'
(HashCode=19976800);目标元素为“网格”(名称=“”);目标
属性为“宽度”(类型为“双精度”)
您正在将DataContext设置为
类型
(x:Type
正在执行此操作)。您已经设置了TargetType
。因此,删除行
,并将绑定
s更改为模板绑定
s,它就会工作。
有关
绑定
和模板绑定
之间差异的更多信息,请查看问题“它不工作”-如何操作?有错误吗?@emperueraiman,该死,我的剪贴板上有错误,但忘了把它放进去!lol将立即添加它完成此操作,但仍不工作,但不再有错误。我使用的控件:
哦,并尝试在模板内使用TemplateBinding。将
更改为
,这是因为您正在为转换器分配字符串。将转换器放入资源中并使用{StaticResource}扩展。如参考资料中所示。: