Right, so I'm following a tutorial. This one, to be exact. Some code that it provides is this:
<TextBlock Margin="2" Foreground="Red" FontWeight="Bold"
Text="{Binding ElementName=AddressBox,
Path=(Validation.Errors),
Converter={StaticResource eToMConverter}}" />
As you can see, it binds to a TextBox
's validation errors, and that TextBox
's x:Name
is AddressBox
. Now, my problem is: I've got a Window
a bit like this one. It also has only one TextBox
. However, I'd rather not use Name
s or x:Name
s if possible. Is it possible to bind to another Control
's Validation.Errors
without being that control being named, and that control being the only one of that type of Control
on that same Window
? The TextBox
is on the same level as the ListBox
.
Other way apart from binding with
ElementName
, is using x:Reference but it also needs the target element to havex:Name
defined on it. So, that's out of scope here.Other approach what I can think of without defining name is to bind something like below (binding to parent and than indexer to get target child).
But this is tightly coupled to your Logical tree structure -
Also, this can be achieved using IValueConverter. As you mentioned that there is only one element of such type in your parent container, you can pass a parent to converter which will traverse the child using VisualTreeHelper class.
Here is your converter code -
Here's the method to traverse using VisualTreeHelper. I have put this method in my Utility class, comes handy in many situations -