2011-12-28 3 views
1

私はカスタムユーザーコントロールを作成していますがスローされます。これには、Propertiesにバインドする2つのDependencyPropertiesがあります。私は私のユーザーコントロールを使用して、バインディングそれが例外をスローしないとき:は例外

System.Windows.Markup.XamlParseException:

「A 『バインディングは』タイプ「AgentPropertyControlの 『PropertyValueを』プロパティに設定することができません

。。バインディング」。A '' が唯一の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 }); 
    } 
} 

そして、私のユーザーコントロールの使用方法:

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

そのDataContextのは、ユーザーコントロールが含まれているグリッドに設定されています。

は、なぜそれが例外をスローして、どのように私はそれを解決できますか? DependencyProperty.Register

答えて

2

第三引数には、所有者のタイプです。

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

ありがとう:あなたのケースでは、それはあなたのコントロールする必要があります!それは問題を解決しました:) –