views:

211

answers:

1

So I have this issue I've been trying various ways to tackle all day and nothing's catching and working for it. Basically I have a XAML object called ChromeWindow (derived from Window) which has in it's code-behind a DependencyProperty called AppChrome which stores a reference to an associated ApplicationChrome XAML object (derived from UserControl). ApplicationChrome's XAML file has a few x:Name'd objects (a TextBlock and Border for instance) to which I want to bind to from the ChromeWindow's XAML. The root of the ChromeWindow is x:Name'd as 'rootWindow' in the XAML, so I figured one of these bindings would work:

{Binding ElementName=rootWindow, Path=AppChrome.CaptionTextBlock.Text, Mode=OneWay}

But that complains of a BindingExpression path error such that the property 'CaptionTextBlock' (an x:Name'd TextBlock in AppChrome's XAML) cannot be found on object of type ApplicationChrome

So I tried this binding intead:

{Binding Source=AppChrome.CaptionTextBlock, Path=Text, Mode=OneWay}

And still no luck, this time complaints of a BindingExpression path error again, but this time that it cannot find the 'CaptionTextBlock' property on object of type String

I know I'm missing something really simple here, please help! ;D

A: 

Even though you have named those elements, they will not be available for binding unless there are dependency properties exposed for them.

ApplicationChrome would need to expose a dependency property called CaptionTextBlock for this binding to work.

Perhaps you could explain why you are trying to bind to these objects, so we could better understand your scenario. There is probably a better way of doing it, because this kind of binding is not only messy but rather unusual.

Charlie
I'm trying to path THROUGH them to their properties as the final destination, not bind them themselves. Do they need to be DPs to bind to their properties too? And if so, is there an easy way to turn an x:Name'd XAML element into a DP to be accessed?
tpartee
Yes but how is WPF supposed to know that there is a CaptionTextBlock on AppChrome? All it knows is that AppChrome is some kind of UserControl, with some kind of visual tree. INSIDE your visual tree you have named an element, but WPF has no idea that exists. It does know about dependency properties exposed on your class, but that is all it knows about.And no, there's not an easy way to do that. Maybe you could describe what you are trying to accomplish through this binding.
Charlie
WPF is supposed to know that the element is there because it's in the XAML for the object (which is one portion of the partial class definition), and x:Name'd elements become members of the class due to this. If IntelliSense can figure it out, then there's no reason the pre-compiler, compiler and linker shouldn't as well.Anyway, this looks more like an issue with the XAML Markup Extension path parser for Bindings - looking at some old code I did for .Net 3.5 SP1 this ued to be possible, but in .Net 4.0 it's choking.
tpartee
No, you are mistaken. When you name elements using the x:Name attribute, those elements are indeed INSTANCED in the visual tree. But they do not become MEMBERS of the class. When you try to bind to AppChrome.CaptionTextBlock, it's going to look at the ApplicationChrome class. That class, which is derived from userControl, does not expose any public property called CaptionTextBlock. The error message describes this exact problem to you: "the property 'CaptionTextBlock' cannot be found on object of type ApplicationChrome." This will never work, and never has.
Charlie