I have some simple Dependency Properties that are not working. I’ve looked over them, looked over code I’ve used in the past, and I’m not sure why they are not working.
I have a custom base class (MyBaseControl) that extends UserControl, and my custom UI controls then extend on that. For example, MyCustomControl extends MyBaseControl.
MyCustomControl XAML is simple enough:
<StackPanel>
<Image Source="{Binding Icon}" />
<TextBlock Text="{Binding Blurb}" />
</StackPanel>
MyCustomControl Code looks like this:
public partial class MyCustomControl: MyBaseControl
{
//public static readonly DependencyProperty IconProperty = Image.SourceProperty.AddOwner(typeof(MyCustomControl));
//public static readonly DependencyProperty BlurbProperty = TextBlock.TextProperty.AddOwner(typeof(MyCustomControl));
public static readonly DependencyProperty IconProperty =
DependencyProperty.Register(
"Icon",
typeof(ImageSource),
typeof(MyCustomControl),
new PropertyMetadata(null));
public static readonly DependencyProperty BlurbProperty =
DependencyProperty.Register(
"Blurb",
typeof(String),
typeof(MyCustomControl),
new PropertyMetadata(null));
public MyCustomControl() : base()
{
InitializeComponent();
}
#region Properties
public ImageSource Icon
{
get { return (ImageSource)GetValue(IconProperty); }
set { SetValue(IconProperty, value); }
}
public String Blurb
{
get { return (String)GetValue(BlurbProperty); }
set { SetValue(BlurbProperty, value); }
}
#endregion Properties
}
Notice that I’ve tried a few different ways to define the DependencyProperty. Neither works.
I call my control in the following way:
<ctrl:MyCustomControl Height="240" VerticalAlignment="Center" HorizontalAlignment="Center" Width="320" Blurb="Slide Show" Icon="pack://application:,,,/Resources/photo_scenery.png" />
If I set the Source or Text directly in the XAML, they show up just fine. The bindings are just not wanting to work properly.
What am I missing that is not allowing my bindings to pass through?
Thank you for any help!
UPDATE: I’ve updated the code based on comments and additional changes I’ve tried.
You are registering Icon property incorrectly. In its registration method you need to specify the DP name i.e. in place of “
IconProperty” it should be “Icon” –Also, try setting RelativeSource in your bindings like this –