So I have a number of XAML pages with various Controls, most of them with a TextBlock indicating the intended content. Like:
<TextBlock x:Name="txbCustomerName"
Text="Customer Name"/>
<TextBox x:Name="txtCustomerName"
Text="{Binding DataObject.CustomerName}"/>
I'm in the process of replacing the TextBlocks with Labels, which would look like this:
<Label x:Name="lblCustomerName"
Content="Customer Name"
Target="{Binding ElementName=txtCustomerName}"/>
<TextBox x:Name="txtCustomerName"
Text="{Binding DataObject.CustomerName}"/>
So far, so good. However, there are Controls that aren't always visible. Accordingly, the associated TextBlock follows suit:
<TextBlock x:Name="txbInvoiceAddressStreet"
Text="Street Name"
Visibility="{Binding DataObject.DifferentInvoiceAddress, Converter={StaticResource BoolToVisibility}}"/>
<TextBox x:Name="txtInvoiceAddressStreet"
Text="{Binding DataObject.InvoiceAddressStreet}"
Visibility="{Binding DataObject.DifferentInvoiceAddress, Converter={StaticResource BoolToVisibility}}"/>
I more or less hoped that the Label's Visibility would be automagically equal to that of its Target by default, but apparently I'll have to work for it. Which is fine, it's my job after all.
This first draft works great:
<Label x:Name="txbInvoiceAddressStreet"
Content="Street Name"
Target="{Binding ElementName=txtInvoiceAddressStreet}"
Visibility="{Binding Path=Visibility, ElementName=txtInvoiceAddressStreet}"/>
<TextBox x:Name="txtInvoiceAddressStreet"
Text="{Binding DataObject.InvoiceAddressStreet}"
Visibility="{Binding DataObject.DifferentInvoiceAddress, Converter={StaticResource BoolToVisibility}}"/>
You'll note that the Binding for my Label's Visibility is linked to the same element as Target instead of targeting the same data element as the TextBlock. I feel it concentrates relevant information in the TextBox instead of spreading it on both controls.
All this is working fine. Still, I can't help feeling that I might take it a step further if I found a way to apply that Binding to the TextBox's property directly through the Label's Target property instead of reusing the TextBox's name.
Like this, except it doesn't work because Source isn't a dependency property:
Visibility="{Binding Path=Visibility, Source={Binding Path=Target, RelativeSource={RelativeSource Self}}}"
As I said, this does not work. However, I hope it conveys a sense of what I'm trying for.
The ultimate step after that, of course, would be to move Visibility to the Labels' default style, so if there's a way to do that I'd like to know about it.