I'm creating a Custom Usercontrol. It has two DependencyProperties I'd like to bind to Properties. When I use my UserControl and do the Binding it throws an exception:
System.Windows.Markup.XamlParseException:
"A 'Binding' cannot be set on the 'PropertyValue' property of type 'AgentPropertyControl'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
I can't figure out what I'm doing wrong.
This is my UserControl XAML code:
<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>
The Bindings are set in 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 });
}
}
And the usage of my UserControl:
<AgentProperty:AgentPropertyControl PropertyTitle="ID" PropertyValue="{Binding Path=ID}" Grid.ColumnSpan="2"/>
Its DataContext is set on the Grid that contains the UserControl.
Why is it throwing the exception and how can I solve it?