gpt4 book ai didi

c# - DependencyProperty 不适用于 CustomControl

转载 作者:行者123 更新时间:2023-11-30 23:33:40 25 4
gpt4 key购买 nike

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

这是我的代码 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 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')

最佳答案

您正在将 DataContext 设置为 Type (x:Type 正在这样做)。您已设置 TargetType已经。所以删除行 <Setter Property="DataContext" Value="{x:Type local:UserProfile}" />并更改 Binding s 至 TemplateBinding s,它将起作用。检查this有关 Binding 之间差异的更多信息的问题和 TemplateBinding

关于c# - DependencyProperty 不适用于 CustomControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33829561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com