在自定义用户控件中绑定到DependencyProperties会抛出异常

本文关键字:DependencyProperties 抛出异常 绑定 自定义 用户 控件 | 更新日期: 2023-09-27 18:19:06

我正在创建一个自定义用户控件。它有两个我想绑定到Properties的DependencyProperties。当我使用UserControl并执行绑定时,它抛出了一个异常:

System.Windows.Markup.XamlParseException:

" 'Binding'不能在'AgentPropertyControl'类型的'PropertyValue'属性上设置。"Binding"只能在DependencyObject的DependencyProperty上设置。

我不知道我做错了什么。

这是我的UserControl XAML代码:
<UserControl x:Class="AgentProperty.AgentPropertyControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="26" d:DesignWidth="288"
             x:Name="MyUserControl">
    <Grid Name="grid">
        <StackPanel Orientation="Horizontal">
            <Label Name="lblPropertyTitle" Width="100" Margin="2" FontWeight="Bold" VerticalAlignment="Center"/>
            <TextBox Name="tbPropertyValue" Width="150" Margin="2" VerticalAlignment="Center"/>
        </StackPanel>
    </Grid>
</UserControl>

绑定在后面代码中设置:

public partial class AgentPropertyControl : UserControl
{
    public readonly static DependencyProperty PropertyTitleDP = DependencyProperty.Register("PropertyTitle", typeof(string), typeof(Label), new FrameworkPropertyMetadata("no data"));
    public readonly static DependencyProperty PropertyValueDP = DependencyProperty.Register("PropertyValue", typeof(string), typeof(TextBox), new FrameworkPropertyMetadata("no data"));
    
    public string PropertyTitle
    {
        set { SetValue(PropertyTitleDP, value); }
        get { return (string) GetValue(PropertyTitleDP); }
    }
    
    public string PropertyValue
    {
        set { SetValue(PropertyValueDP, value); }
        get { return (string)GetValue(PropertyValueDP); }
    }
    public AgentPropertyControl()
    {
        InitializeComponent();
        lblPropertyTitle.SetBinding(Label.ContentProperty, new Binding() {Source = this, Path = new PropertyPath("PropertyTitle")});
        tbPropertyValue.SetBinding(TextBox.TextProperty, new Binding() { Source = this, Path = new PropertyPath("PropertyValue"), Mode = BindingMode.TwoWay });
    }
}

和UserControl的用法:

<AgentProperty:AgentPropertyControl PropertyTitle="ID" PropertyValue="{Binding Path=ID}" Grid.ColumnSpan="2"/>

它的DataContext设置在包含UserControl的网格上。

为什么抛出异常,我该如何解决它?

在自定义用户控件中绑定到DependencyProperties会抛出异常

DependencyProperty.Register的第三个参数是所有者类型。在你的情况下,它应该是你的控制:

public readonly static DependencyProperty PropertyTitleDP = DependencyProperty.Register("PropertyTitle", typeof(string), typeof(AgentPropertyControl), new FrameworkPropertyMetadata("no data"));
public readonly static DependencyProperty PropertyValueDP = DependencyProperty.Register("PropertyValue", typeof(string), typeof(AgentPropertyControl), new FrameworkPropertyMetadata("no data"));
相关文章:
  • 没有找到相关文章