1

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.

2 Answers 2

4

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" -

public static readonly DependencyProperty IconProperty = DependencyProperty.Register( "Icon", typeof(ImageSource), typeof(MyCustomControl), new PropertyMetadata(null)); 

Also, try setting RelativeSource in your bindings like this -

<StackPanel> <Image Source="{Binding Icon, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ctrl:MyCustomControl}}}" /> <TextBlock Text="{Binding Blurb, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ctrl:MyCustomControl}}}" /> </StackPanel> 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks RV1987. I was so ready to shake my fist in the air for missing that, but it looks like I have at least 2 problems with my code. I made the above change, but am still not seeing my image.
I have updated the answer. You need to specify the relativeSource for your bindings.
The RelativeSource on the bindings did it. Thank you! I'm going to need to read up again on what all that is doing -- as I'm not understanding the need for them, but it works for now! Thanks again!
0

Alternatively, instead of setting RelativeSource as suggested by Rohit Vats, you may be able to solve the issue by updating your code as follows

<StackPanel> <Image Name="imgImage" Source="{Binding Icon}" /> <TextBlock Name="txtTextBlock" Text="{Binding Blurb}" /> </StackPanel> 

and

public LabeledTextBox() { InitializeComponent(); imgImage.DataContext = this; txtTextBlock.DataContext = this; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.