0

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.

1
  • Thanks ASh, worked wonders! I see you replaced the "target" tag with "xaml". I can see your point since this is very much XAML, but the already existing "wpf" tag, along with "binding", makes it pretty clear that we're talking XAML, whereas "target" seems more specific, so I think I'd rather keep it. Could you elaborate on your reasoning, please? Commented Oct 22, 2018 at 12:06

1 Answer 1

0

include Target property in binding path:

<Label Visibility="{Binding Path=Target.Visibility, RelativeSource={RelativeSource Self}}"/>

and it can be a part of Style:

<Style TargetType="Label">
    <Setter Property="Visibility"
            Value="{Binding Path=Target.Visibility, RelativeSource={RelativeSource Self}}"/>
</Style>

Not the answer you're looking for? Browse other questions tagged or ask your own question.