I'm trying to create a custom usercontrol in WPF, that includes a textbox and some other controls. I want to be able to bind a string property from the datacontext to my usercontrol, that is then passed on to the TextBox within my usercontrol.
If it was a regular textbox, I would do this to bind the property FirstName
of the object in the DataContext
:
<TextBox Text="{Binding FirstName}" />
In my CustomTextBox
, I would like to be able to do this:
<local:CustomTextBox CustomText="{Binding FirstName}"/>
But I can't quite get the databinding to work.
Codebehind for my CustomTextBox
:
public partial class CustomTextBox : UserControl
{
public CustomTextBox()
{
InitializeComponent();
}
public static readonly DependencyProperty CustomTextProperty =
DependencyProperty.Register(
"CustomText",
typeof(string),
typeof(CustomTextBox));
public string CustomText
{
get
{
return (string)GetValue(CustomTextProperty);
}
set
{
SetValue(CustomTextProperty, value);
}
}
}
Xaml markup for CustomTextBox
:
<Grid>
<TextBox x:Name="tb" Text="{Binding CustomText}"></TextBox>
</Grid>
What am I missing?