【发布时间】: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,废话,我的剪贴板中有错误,但忘了把它放进去!大声笑现在会添加它