I have a user control which I’d like to be used like so:
// MainPage.xaml
<my:MyControl Data="10" />
<!-- or -->
<my:MyControl Data="{Binding SomeData}" />
It’s codebind is this:
public partial class MyControl : UserControl
{
public MyControl() {
InitializeComponent();
}
public const string DataPropertyName = "Data";
public int Data
{
get
{
return (int)GetValue(DataProperty);
}
set
{
SetValue(DataProperty, value);
}
}
public static readonly DependencyProperty DataProperty = DependencyProperty.Register(
DataPropertyName,
typeof(int),
typeof(MyControl),
new PropertyMetadata(10);
}
It’s xaml portion is this:
<UserControl>
<!-- omitted namespaces etc. -->
<Grid x:Name="LayoutRoot">
<Button x:Name="myButton" Content="{Binding Data}">
<Button.Style>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<TextBlock Text="{TemplateBinding Content}" />
</ControlTemplate>
</Setter.Value>
</Button.Style>
</Button>
</Grid>
</UserControl>
The crucial line, in usercontrol’s xaml part is:
<Button x:Name="myButton" Content="{Binding Data}">
I’d like to bind this Button’s Content property to the UserControl’s property (Data), while still retaining the ability to set values on it from outside (<my:MyControl Data="10" />)
The problem is, that when I use binding – <Button x:Name="myButton" Content="{Binding Data}"> – it doesn’t work (The templatebinding doesnt pick any values)
It works however, if I set the values manually i.e – <Button x:Name="myButton" Content="12">
If you want to bind to your “own” dependency property inside a
UserControlyou need to add ax:Nameto yourUserControland use it as theElementNamein your binding.To make the
Templatealso work:Instead of the
TemplateBindingyou need to use theRelativeSource TemplatedParentsysntax, because you need to set theMode=OneWay(TemplateBinding usesMode=OneTimefor performance reasons by default but in your scenario you needMode=OneWay)