Aleksandar Toplek
Aleksandar Toplek

Reputation: 2831

TemplateBinding in Binding Path

I have an Custom Control with dependency property Header. In Generic.xaml there is a style for that control. I want to set visibility of some part of the template so that it depends on Header property from the control.

<Style TargetType="{x:Type CustomControl1}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type CustomControl1}">
                <Grid MinWidth="400" Focusable="False">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>

                    <TextBlock Text="{TemplateBinding Header}" 
                               Style="{DynamicResource SomeStyle}" 
                               Margin="0,0,80,0"
                               Grid.Row="0" Grid.Column="0">
                        <TextBlock.Visibility>



                            <Binding Path="{TemplateBinding Header}">

                            or alternatively

                            <Binding Path="{Binding Path=Header, RelativeSource={RelativeSource TemplatedParent}}">



                                <Binding.Converter>
                                    <converters:ValueConverterGroup>
                                        <converters:StringNullOrEmptyToBooleanConverter />
                                        <BooleanToVisibilityConverter />
                                    </converters:ValueConverterGroup>
                                </Binding.Converter>
                            </Binding>
                        </TextBlock.Visibility>
                    </TextBlock>

                   ...

This line throws an exception:

<Binding Path="{Binding Path=Header, RelativeSource={RelativeSource TemplatedParent}}">

According to the exception message there can't be a Binding inside Path property of Binding (I already knew that) xD

How could this be done without double-binding? How to access property of Custom Control without using TemplateBinding?

Upvotes: 1

Views: 2113

Answers (1)

King King
King King

Reputation: 63317

Path property of a Binding is not a DependencyProperty, I doubt you may want it to be like this:

<TextBlock.Visibility>
   <Binding Path="Header" RelativeSource="{RelativeSource TemplatedParent}">
          <Binding.Converter>
              <converters:ValueConverterGroup>
                   <converters:StringNullOrEmptyToBooleanConverter />
                    <BooleanToVisibilityConverter />
              </converters:ValueConverterGroup>
          </Binding.Converter>
   </Binding>
</TextBlock.Visibility>

Upvotes: 3

Related Questions