绑定到Custom Usercontrol中的DependencyProperties会引发异常

时间:2011-12-28 09:42:02

标签: c# wpf data-binding exception-handling user-controls

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

  

System.Windows.Markup.XamlParseException:

     

“A'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>

Bindings在Code Behind中设置:

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的网格上设置。

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

1 个答案:

答案 0 :(得分:2)

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"));
相关问题