Проблемы с привязкой свойства переднего плана GroupBox HeaderTemplate

StackOverflow https://stackoverflow.com/questions/967551

  •  12-09-2019
  •  | 
  •  

Вопрос

Я определил DataTemplate для заголовка моих групповых полей:

   <Style x:Key="GroupBoxHeaderStyle" TargetType="{x:Type GroupBox}">
        <Setter Property="Foreground" Value="White"></Setter>
        <Setter Property="HeaderTemplate">
            <Setter.Value>
                <DataTemplate>                       
                    <TextBlock Text="{Binding}" Foreground="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Foreground}"
                               FontSize="18" FontFamily="Global User Interface"/>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Однако привязка, выполненная для переднего плана, похоже, не работает.Заголовки моих групповых полей всегда черные.Что я делаю не так?

Вот как я определяю GroupBox:

<GroupBox Header="Views" Margin="1" Foreground="White"
          Style="{StaticResource GroupBoxHeaderStyle}">
          ...
Это было полезно?

Решение

...
<TextBlock Text="{Binding}" Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}, Path=Foreground}"
           FontSize="18" FontFamily="Global User Interface"/>
...
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top