Geeks With Blogs
New Things I Learned

Yesterday, I have some ideas in my mind on how to give out ValidationRule errors after the values passes the converter, and I was trying to see if I can use Attached Properties for that.

When trying to do this, one of the things I'd like to do was set a control's text to the Attached Property's value and that's when I realized that I don't know the syntax to do that.

A quick Google search yields some results; someone asked the same question and the answer suggested using parentheses.  The following example where the TextBox will display the row index the textbox is residing show how this works:

Bind to Grid.Row attached property

<Grid>

    <Grid.RowDefinitions>

        <RowDefinition Height="Auto"/>

        <RowDefinition Height="Auto"/>

        <RowDefinition Height="Auto"/>

        <RowDefinition Height="Auto"/>

    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>

        <ColumnDefinition/>

    </Grid.ColumnDefinitions>

    <TextBox Text="{Binding (Grid.Row),RelativeSource={RelativeSource Self}}"

             Height="23" Grid.Column="0" Grid.Row="0" VerticalAlignment="Top" />

    <TextBox Text="{Binding (Grid.Row),RelativeSource={RelativeSource Self}}"

             Height="23" Grid.Column="0" Grid.Row="1" VerticalAlignment="Top" />

    <TextBox Text="{Binding (Grid.Row),RelativeSource={RelativeSource Self}}"

             Height="23" Grid.Column="0" Grid.Row="2" VerticalAlignment="Top" />

    <TextBox Text="{Binding (Grid.Row),RelativeSource={RelativeSource Self}}"

             Height="23" Grid.Column="0" Grid.Row="3" VerticalAlignment="Top"

             Name=”textBox4” />

</Grid>

You can also see that I didn't use Path=(Grid.Row), by default the first parameter is assigned to the Path property of the Binding class.

What about if we want to do it in code; it's very similar: 

Binding b = new Binding("(Grid.Row)");

b.Source = textBox4;

BindingOperations.SetBinding(textBox4, TextBox.TextProperty, b);

The binding path is the same; (Grid.Row) and the other important tidbit is that the source is the textbox itself.  This is logical since when assigning a value to an Attached Property, you're assigning that value for a given DependencyObject.  In this case, we want the text to be set to the Grid.Row attached property for that particular textbox too.

Verifying that it works, now I continue on with my class that implements the Attached Property.  However, since I need to refer to my class using XML namespace, how would the Path look?  The path has to use the namespace too, as the sample below shows:

 

<Grid xmlns:local="clr-namespace:BindingToAttachedProperty">

    <Grid.RowDefinitions>

        <RowDefinition Height="Auto"/>

        <RowDefinition Height="Auto"/>

        <RowDefinition Height="Auto"/>

        <RowDefinition Height="Auto"/>

    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>

        <ColumnDefinition/>

    </Grid.ColumnDefinitions>

    <TextBox Text="{Binding Path=(local:LocalClass.SomeValue),RelativeSource={RelativeSource Self}}"

             local:LocalClass.SomeValue="1"

             Height="23" Grid.Column="0" Grid.Row="0" Vertical.Alignment="Top" />

    <TextBox Text="{Binding (local:LocalClass.SomeValue),RelativeSource={RelativeSource Self}}"

             local:LocalClass.SomeValue="2"

             Name="textBox2"

             Height="23" Grid.Column="0" Grid.Row="1" VerticalAlignment="Top" />

</Grid>

I found a weird condition in WPF; you can see that for the first TextBox, the Binding has the Path=, while the second one doesn't.  The sample above works, the first textbox will display 1, and the second textbox will display 2.  However, if you take out that Path= string, the example will not work; the BindingExpression specifies PathError.  I believe this is because without the first Path=, WPF doesn't know what is (local:LocalClass.SomeValue).  By specifying Path=, WPF compiles it correctly, and the subsequent encounter of (local:LocalClass.SomeValue) in textBox2 results in a correct interpretation.

OK, the XAML works; how to do it in code?  I tried all kinds of combination for the path, and could not get anything to work - it took me about 2 hours trying around and finally I got a solution.

Binding b = new Binding();

b.Path = new PropertyPath(LocalClass.SomeValueProperty);

b.Source = textBox2;

BindingOperations.SetBinding(textBox2, TextBox.TextProperty, b);

Most of the time, people will just create a new Binding instance, passing the string path in its constructor.  I could not get this to work; all types of string values that I tried results in PathError.  Finally, I took a good look at the Path property in the Binding class and found that one of the PropertyPath constructor accepts an object parameter that can be a DependencyProperty.

This would be my first 'New Thing I Learned' to document .  I'm sure I will look at this information again in the future, and I hope other people can benefit from this.

Posted on Tuesday, January 15, 2008 12:53 PM WPF , .NET | Back to top


Comments on this post: Binding to an Attached Property

# re: Binding to an Attached Property
Requesting Gravatar...
This post is a life saver. I spent hours (literally) on this stupid binding hiccup.

Thank you, thank you, thank you.
Left by Matthias on Feb 02, 2008 12:20 AM

# Thank you a lot!
Requesting Gravatar...
This post helped me.
Left by Fame T. on Oct 14, 2008 7:35 AM

# re: Binding to an Attached Property
Requesting Gravatar...
Very interesting - thanks for blogging this.
Left by Jim on Jan 04, 2009 4:50 PM

# re: Binding to an Attached Property
Requesting Gravatar...
Thanks man!!!
Left by Peter on Mar 13, 2009 8:44 AM

# re: Binding to an Attached Property
Requesting Gravatar...
You're the man!
Left by Bob on Apr 07, 2009 9:03 AM

# re: Binding to an Attached Property
Requesting Gravatar...
are you a god?
Left by devzi11a on Apr 23, 2009 11:24 AM

# re: Binding to an Attached Property
Requesting Gravatar...
You're amazing. Thanks, I was an hour and half into this problem.

My need was slightly different I needed to set a relative binding value to an attached behavior dependency property.

This ended up being the solution:

// Returns the first Popup control encountered on the upward path
// starting at the target element of the binding
var myBinding = new Binding
{
Mode = BindingMode.OneWay,
RelativeSource = new RelativeSource
{
AncestorLevel = 1,
AncestorType = typeof (Popup),
Mode = RelativeSourceMode.FindAncestor
}
};




BindingOperations.SetBinding(this, PopupControlProperty, myBinding);
Left by Joe Gershgorin on Jul 12, 2009 5:18 PM

# re: Binding to an Attached Property
Requesting Gravatar...
Very nice, helped me to my exact problem. This is a post I like, not much "blabla" straight to the point. Bravo!
Left by Daniell on Mar 10, 2010 8:36 PM

# re: Binding to an Attached Property
Requesting Gravatar...
good work.. MSdata man.
Left by rahul kapoor on Mar 22, 2010 4:49 AM

# re: Binding to an Attached Property
Requesting Gravatar...
This must be for WPF because the same doesn't work for Silverlight 3. Been pulling my hair out to find a solution for this same problem in SL3.
Left by Brad on Apr 07, 2010 7:04 AM

# re: Binding to an Attached Property
Requesting Gravatar...
In regard to the little bug regarding the Path argument - I've also expereinced that issue when binding to indexed properties belonging to objects that are not defined in the default xml namespace - in other words custom classes.
Left by JD on Jun 01, 2010 2:01 AM

# re: Binding to an Attached Property
Requesting Gravatar...
Beautiful... thank you!
Left by Ian Randall on Jun 17, 2010 3:48 PM

# re: Binding to an Attached Property
Requesting Gravatar...
Thank you! That's awesome!
It saved me alot of time!
Left by Vordoom on Aug 03, 2010 9:40 PM

# re: Binding to an Attached Property
Requesting Gravatar...
awesome, exactly what i was looking for ;)
Left by krishna on Dec 07, 2010 8:15 PM

# re: Binding to an Attached Property
Requesting Gravatar...
thumbs up - you saved me:)
Left by Nick on Apr 05, 2011 12:09 PM

# re: Binding to an Attached Property
Requesting Gravatar...
Can you please share the complete source code for this? A silverlight version will be more useful.
Left by Lijo on Jun 10, 2011 1:25 AM

# re: Binding to an Attached Property
Requesting Gravatar...
It was helpful and saved me some debugging - thanks. :)
Left by Steve on Jun 21, 2011 7:59 AM

# re: Binding to an Attached Property
Requesting Gravatar...
Thank you!! Yes!! Banging my head against the wall for hours, now you got me past that one.
Left by Jon on Nov 26, 2011 12:54 PM

# re: Binding to an Attached Property
Requesting Gravatar...
Tip Top!!! Thx
Left by Jochen on Feb 09, 2012 2:03 AM

# re: Binding to an Attached Property
Requesting Gravatar...
Hi
can u share the complete code then it is useful for me ... Good work keep it up...:)
Left by Murugan on Jul 20, 2012 4:58 AM

# re: Binding to an Attached Property
Requesting Gravatar...
Nice One, Thanks!
Left by Suresh Thotakura on Aug 19, 2012 7:20 AM

# re: Binding to an Attached Property
Requesting Gravatar...
Thanks, just what I needed.
Left by Alex on Sep 05, 2012 4:30 AM

Your comment:
 (will show your gravatar)


Copyright © Muljadi Budiman | Powered by: GeeksWithBlogs.net | Join free