【问题标题】:Binding to DependencyProperties in Custom Usercontrol throws exception绑定到自定义用户控件中的 DependencyProperties 引发异常
【发布时间】:2012-01-29 01:33:38
【问题描述】:

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

System.Windows.Markup.XamlParseException:

“不能在“AgentPropertyControl”类型的“PropertyValue”属性上设置“Binding”。“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 的 Grid 上。

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

【问题讨论】:

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


    【解决方案1】:

    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"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-13
      • 2011-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多