0

Referencing to this question: WPF Databinding: How do I access the "parent" data context?

I wanna do something similiar, but for the header of a Groupbox (because the header does not concern with the Box is being disabled and thus is always black while the rest is light gray. This looks a bit strange to me if all the content of the box is gray, the above is gray, but the box title itself stays black.

So I tried to use the approach mentioned in the linked question by flq to simply bind the isEnabled property of the header textblock to the isEnabled property of the groupbox but it seems that my binding at some point fails and I don't know where and why exactly.

heres my current code:

<GroupBox Header="Change Steps" Grid.Row="2" Grid.ColumnSpan="3" Name="gbChangeSteps">
     <GroupBox.Style>
          <Style TargetType="GroupBox">
              <Setter Property="HeaderTemplate">
                  <Setter.Value>
                      <DataTemplate>
                          <TextBlock Text="{Binding}" FontWeight="Bold" Height="19" Foreground="Black" IsEnabled="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}, Path=isEnabled}"/>
                      </DataTemplate>
                  </Setter.Value>
              </Setter>
          </Style>
      </GroupBox.Style>
<!-- ... (some non relevant Content)-->
</GroupBox>
Community
  • 1
  • 1
lsteinme
  • 750
  • 1
  • 6
  • 20

1 Answers1

0

after additional research I found the post Disable groupBox including the groupBox name in WPF that lead me, in combination with Properties->Create Databinding->Binding type->UIElement to the solution that fixed both problems, the one this question was about and the original one that lead to entire restyling, which was that letters like the small g got messed up in the header. This is the code that fixed the issue:

<GroupBox.Style>
   <Style TargetType="{x:Type GroupBox}">
       <Setter Property="HeaderTemplate">
          <Setter.Value>
               <DataTemplate>
                   <TextBlock Text="{Binding}" FontWeight="Bold" Height="19" IsEnabled="{Binding IsEnabled, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UIElement}}}">
                       <TextBlock.Style>
                           <Style>
                               <Style.Triggers>
                                   <Trigger Property="Control.IsEnabled" Value="False">
                                       <Setter Property="Control.Foreground" Value ="#FF6D6D6D" />
                                   </Trigger>
                               </Style.Triggers>    
                           </Style>
                      </TextBlock.Style>
                   </TextBlock>
               </DataTemplate>
           </Setter.Value>
       </Setter>
     </Style>
</GroupBox.Style>
Community
  • 1
  • 1
lsteinme
  • 750
  • 1
  • 6
  • 20