开发者

WPF templating/styling issue

Given this piece of XAML

<DockPanel>
  <DockPanel.Resources>
    <Style TargetType="{x:Type GroupBox}">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type GroupBox}">
  开发者_C百科          <DockPanel>
              <Border DockPanel.Dock="Top">
                <Border.Resources>
                  <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="Foreground"
                        Value="Red" />
                  </Style>
                </Border.Resources>
                <ContentPresenter ContentSource="Header" />
              </Border>
              <ContentPresenter />
            </DockPanel>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </DockPanel.Resources>

  <GroupBox VerticalAlignment="Top"
      Header="GroupBox header"
      DockPanel.Dock="Top">

    ...
    ...

I would like to know why the group box header is not displayed in red letters.

I've already tried styling the Label type with no success either.

(sorry about the overly generic post title... I wasn't able to think of something more meaninful)


This code solved the problem:

<DockPanel>
  <DockPanel.Resources>
    <Style TargetType="{x:Type GroupBox}">
      <Setter Property="HeaderTemplate">
        <Setter.Value>
          <DataTemplate>
            <DataTemplate.Resources>
              <Style TargetType="Label">
                <Style.Setters>
                  <Setter Property="Foreground" Value="Red" />
                </Style.Setters>
              </Style>
            </DataTemplate.Resources>
            <Label Content="{Binding}" />
          </DataTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </DockPanel.Resources>

  <GroupBox VerticalAlignment="Top" Header="GroupBox header" DockPanel.Dock="Top">
  ...
  ...

However, I still don't know why the proposed code didn't worked.


It seems that the ContentPresenter doesn't use TextBlock to show the string you provide as header or explicitly sets its style, so the style you defined cannot be applied.

If you are certain that you will only use text as group box header, you can remove the ContentPresenter and use a TextBlock on your own.

  <DockPanel>
  <DockPanel.Resources>
    <Style TargetType="{x:Type GroupBox}">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type GroupBox}">
            <DockPanel>
              <Border DockPanel.Dock="Top">
                <Border.Resources>
                  <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="Foreground" Value="Red" />
                  </Style>
                </Border.Resources>
                <TextBlock Text="{TemplateBinding Header}"></TextBlock>
              </Border>
              <ContentPresenter />
            </DockPanel>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </DockPanel.Resources>

  <GroupBox VerticalAlignment="Top"
      Header="GroupBox header"
      DockPanel.Dock="Top"/>
  </DockPanel>


try this:

<DockPanel.Resources>
    <Style TargetType="{x:Type GroupBox}" >
        <Setter Property="Foreground" Value="Red" />
    </Style>
</DockPanel.Resources>

You don't need a templet for this. But if you demand on using a Templete, you probably have to set the Groupbox.HeaderTemplet not the GroupBox.Templet.

Edit:

This is what i got so far, but i keep getting an XamlPraseException.

<Style TargetType="{x:Type GroupBox}" >
    <Setter Property="HeaderTemplate">
        <Setter.Value>
            <ControlTemplate>
                <StackPanel>
                    <StackPanel.Resources>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="Foreground" Value="Red"/>
                        </Style>
                    </StackPanel.Resources>
                    <TextBlock Text="{TemplateBinding GroupBox.Header}" />
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜