2011-12-28 2 views
1

사용자 지정 Usercontrol을 만들고 있습니다. 그것은 Properties에 bind하고 싶은 두 개의 DependencyProperties를 가지고 있습니다. 내 UserControl을을 사용하면 수행에 예외를 throw하는 바인딩 :사용자 지정 UserControl의 DependencyProperties에 대한 바인딩이 예외를 throw합니다.

System.Windows.Markup.XamlParseException :

'A'타입의 '성질 값'속성을 설정할 수 없습니다 '바인딩'AgentPropertyControl '. A'바인딩 '는 단지 DependencyObject에의 DependencyProperty를 설정할 수 있습니다

내가 잘못 뭘하는지 알아낼 수 없습니다

를이 내 UserControl을 XAML 코드입니다..

,451,515,
<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을 포함하는 그리드에 설정되어 있습니다.

왜 예외가 발생하며 어떻게 해결할 수 있습니까?

답변

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

감사합니다! 그게 문제를 해결 :) –

관련 문제