I have a WPF UserControl to which I added a new string
dependency property. I want to access that property from another control (Image) within the ControlTemplate defined for the UserControl on it's own XAML page. The string
will be set on the Source property of the Image in the ControlTemplate. Setting the property works but the control in the ControlTemplate is not receiving the value. Why would that be?
Here's my code:
public static readonly DependencyProperty ButtonImageSourceProperty = DependencyProperty.Register("ButtonImageSource", typeof(string), typeof(MyControl));
public string ButtonImageSource
{
get { return (string)GetValue(ButtonImageSourceProperty); }
set { SetValue(ButtonImageSourceProperty, value); }
}
And here's the XAML:
<UserControl.Resources>
<Style TargetType="custom:MyControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="custom:MyControl">
<Grid>
<ContentPresenter></ContentPresenter>
<Image Source="{TemplateBinding ButtonImageSource}" Width="30" Height="30"></Image> <!--Image's source to be set by dependency property-->
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>