Tim Hibbard

Software Architect for EnGraph software
posts - 615, comments - 673, trackbacks - 469

My Links

News



Add to Google




Twitter












Tag Cloud

Article Categories

Archives

Post Categories

Image Galleries

EnGraph Blogs

Links

Other

Roll

WPF Custom Control Dependency Property Gotcha

Let's say you have a custom WPF control called SearchTextBox.  It has a textbox and a button labeled "search".  Simple enough, you reuse it in your application when you want to provide search. 

Then one day, you decide you need this control needs to be bindable.  So you expose a public property Text and map it to textSearch just like you would in WinForms.

Well, that doesn't work, so you google around and stumble upon Dependency Properties and learn how to create your own (VS snippet shortcut propdb) and create a Text DP.

Now you spend 30 minutes trying to map your Text DP to your textSearch.Text until you finally figure out that your DP snippet lead you astray and there is one more step that didn't get included in the shortcut.  In the UIPropertyMetaData, you need to specify a function to call when the property changes - so you can set textSearch.Text.

The function looks like this:

static void textChangedCallBack(DependencyObject property, DependencyPropertyChangedEventArgs args) { SearchTextBox searchTextBox = (SearchTextBox)property; searchTextBox.textSearch.Text= (string)args.NewValue; }

And the rest of the DP looks like this:

public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } } public static readonly DependencyProperty TextProperty = DependencyProperty.Register( "Text", typeof(string), typeof(SearchTextBox), new UIPropertyMetadata(string.Empty, new PropertyChangedCallback(textChangedCallBack)));

 

The important part here is what wasn't created by the VS snippet :

new UIPropertyMetadata(string.Empty, new PropertyChangedCallback(textChangedCallBack))

Now you are binding to your custom control and all is good.

 

Print | posted on Tuesday, April 22, 2008 1:11 AM | Filed Under [ .NET WPF ]

Feedback

Gravatar

# re: WPF Custom Control Dependency Property Gotcha

Umm, why should you need to do this? The binding should work perfectly well without it.
5/15/2008 12:38 PM | Dmitri
Gravatar

# re: WPF Custom Control Dependency Property Gotcha

Actually, it doesn't. I've tried :)
5/15/2008 12:42 PM | Tim Hibbard
Gravatar

# re: WPF Custom Control Dependency Property Gotcha

Are you sure a binding doesn't work? I had a similar issue with a User Control and found that the binding has to include the ElementName.

First, add an x:Name attribute to your UserControl tag (say, with a value of "Main"). Then your binding will look like this:

<TextBox Text="{Binding ElementName=Main,Path=SearchText}"/>

No need for any DP change event code.
5/15/2008 2:46 PM | Mike
Gravatar

# re: WPF Custom Control Dependency Property Gotcha

My example was for people that needed to bind their business objects to a user control, which requires a DP...unless I'm wrong.
5/15/2008 3:42 PM | Tim Hibbard
Gravatar

# re: WPF Custom Control Dependency Property Gotcha

Mike's thing works well. The thing I missed was adding a X:Name to the <Window.
5/20/2008 1:24 PM | Nick
Gravatar

# re: WPF Custom Control Dependency Property Gotcha

Thanks very much Tim. Took me forever to figure out how to do this!

If want to re-use the callback function you can fetch the argument name like so:

if (args.Property.Name.Equals("Text"))
{
searchTextBox.textSearch.Text= (string)args.NewValue;
}

7/29/2008 12:38 AM | Lach
Gravatar

# re: WPF Custom Control Dependency Property Gotcha

Thank you Tim!

I spent most of yesterday trying to solve this problem, and looking at your solution it makes sense.. just wish I found your post a bit earlier :-)
3/17/2009 5:17 AM | Chris
Gravatar

# re: WPF Custom Control Dependency Property Gotcha

You can't access the property in the constructor. You need to get it in the UserControl_Loaded event.
3/20/2009 2:58 AM | Alex Kwiatkowski
Gravatar

# re: WPF Custom Control Dependency Property Gotcha

I've tried all those methods, they work well my Main problem is that its OneWay binding .(ie the textbox in UserControl changes its text when the property in my class changes, but typing in the text box dosen't change the underlaying property.)
This is my 11th day on this, pls i really need help. Thanx alot.
9/9/2009 5:47 AM | Majeed
Gravatar

# re: WPF Custom Control Dependency Property Gotcha

Dmitri is correct - if you just want to bind to the property you don't need to do this as binding in inherent in dependency properties
See:http://www.i-programmer.info/programming/wpf-workings/443-inside-dependency-properties-.html
10/29/2009 8:14 AM | Mike
Gravatar

# re: WPF Custom Control Dependency Property Gotcha

wow ... wish i would have found this post yesterday ! i have been abusing google for this for some time. i got mike's sugestion to work for me ... thanks guys. loads of infomation.
11/3/2009 10:47 AM | John Little
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 

Powered by: