WPF GroupBox ControlTemplate:如何将样式仅应用于标题中的元素?

发布于 2024-09-18 13:35:51 字数 3335 浏览 10 评论 0原文

我正在尝试为 GroupBox 制作一个 ControlTemplate ,这样如果在 Header 中声明了 TextBlock ,它的背景 应设置为黄色。

问题是,尽管我在 ContentPresenter 中为 Header 定义了 TextBlock 的样式,但除了那些 之外,它并未应用。由 WPF 自动生成的 TextBlock

代码如下:

<Window
  x:Class="TestHeaderTemplate.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Window1"
  SizeToContent="WidthAndHeight">
  <Window.Resources>    
    <Style
      TargetType="{x:Type GroupBox}">
      <Setter
        Property="Template">
        <Setter.Value>
          <ControlTemplate
            TargetType="{x:Type GroupBox}">            
            <Border
              Margin="{TemplateBinding Margin}"
              BorderBrush="Black"
              BorderThickness="1">
              <StackPanel>
                <Border
                  Margin="0,0,0,5"
                  BorderThickness="5"
                  BorderBrush="LightBlue"
                  >
                  <ContentPresenter
                    ContentSource="Header">
                    <ContentPresenter.Resources>
                      <Style
                        TargetType="{x:Type TextBlock}">
                        <Setter
                          Property="Background"
                          Value="Yellow" />
                      </Style>
                    </ContentPresenter.Resources>
                  </ContentPresenter>
                </Border>
                <ContentPresenter
                  ContentSource="Content" />
              </StackPanel>
            </Border>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </Window.Resources>
  <StackPanel>
    <TextBox
      Text="All TextBoxes in a GroupBox's Header should be yellow, whether declared or autogenerated." />
    <GroupBox
      x:Name="firstGroupBox"
      Margin="5"
      Header="I am a TextBlock autogenerated by WPF. Since I'm in the Header, I should be yellow.">
        <TextBlock
          Text="I'm a TextBlock declared in the content of the GroupBox. I should NOT be yellow." />      
    </GroupBox>
    <GroupBox
      x:Name="secondGroupbox"
      Margin="5"
      >
      <HeaderedContentControl.Header>      
          <TextBlock
            x:Name="notStyledTextBlock"
            Text="I'm a TextBlock declared in the header. I should be yellow since I'm in the header."
            VerticalAlignment="Center" />          
      </HeaderedContentControl.Header>
      <TextBlock
        Text="I'm declared in the content so I should not be yellow." />
    </GroupBox>
  </StackPanel>
</Window>

如果您尝试一下,您会看到,第二个 GroupBox 中名为 notStyledTextBlockTextBlock 的背景不是黄色,这表示未应用 ControlTemplate 中的 ContentPresenter 资源中定义的样式。

令人惊讶的是,WPF 自动生成的第一个 GroupBox 标题文本的容器的背景为黄色。

我该怎么做才能将我的样式应用于 notStyledTextBlock TextBlock

I'm trying to make a ControlTemplate for a GroupBox such that if a TextBlock is declared in the Header its Background should be set to yellow.

The problem is that, although I define a style for TextBlocks in the ContentPresenter for the Header, it's not applied except on those TextBlocks which are autogenerated by WPF.

Here is the code :

<Window
  x:Class="TestHeaderTemplate.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Window1"
  SizeToContent="WidthAndHeight">
  <Window.Resources>    
    <Style
      TargetType="{x:Type GroupBox}">
      <Setter
        Property="Template">
        <Setter.Value>
          <ControlTemplate
            TargetType="{x:Type GroupBox}">            
            <Border
              Margin="{TemplateBinding Margin}"
              BorderBrush="Black"
              BorderThickness="1">
              <StackPanel>
                <Border
                  Margin="0,0,0,5"
                  BorderThickness="5"
                  BorderBrush="LightBlue"
                  >
                  <ContentPresenter
                    ContentSource="Header">
                    <ContentPresenter.Resources>
                      <Style
                        TargetType="{x:Type TextBlock}">
                        <Setter
                          Property="Background"
                          Value="Yellow" />
                      </Style>
                    </ContentPresenter.Resources>
                  </ContentPresenter>
                </Border>
                <ContentPresenter
                  ContentSource="Content" />
              </StackPanel>
            </Border>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </Window.Resources>
  <StackPanel>
    <TextBox
      Text="All TextBoxes in a GroupBox's Header should be yellow, whether declared or autogenerated." />
    <GroupBox
      x:Name="firstGroupBox"
      Margin="5"
      Header="I am a TextBlock autogenerated by WPF. Since I'm in the Header, I should be yellow.">
        <TextBlock
          Text="I'm a TextBlock declared in the content of the GroupBox. I should NOT be yellow." />      
    </GroupBox>
    <GroupBox
      x:Name="secondGroupbox"
      Margin="5"
      >
      <HeaderedContentControl.Header>      
          <TextBlock
            x:Name="notStyledTextBlock"
            Text="I'm a TextBlock declared in the header. I should be yellow since I'm in the header."
            VerticalAlignment="Center" />          
      </HeaderedContentControl.Header>
      <TextBlock
        Text="I'm declared in the content so I should not be yellow." />
    </GroupBox>
  </StackPanel>
</Window>

As you can see if you try, the background of the TextBlock named notStyledTextBlock in the second GroupBox is not yellow, which means the style defined in the resources of the ContentPresenter in the ControlTemplate is not applied.

Surprisingly, the one that was autogenerated by WPF as a container for the header text of the first GroupBox has its background yellow.

What can I do to make it so my style is applied to the notStyledTextBlock TextBlock?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

能怎样 2024-09-25 13:35:51

我也遇到了 GroupBoxes 和 ContentPresenter 的问题。我发布了一个问题,因为没有给出答案,所以我对自己进行了一些调查。看看这个答案,也许是同样的问题(附加信息:我没有发布我真正的问题代码,而是一个可用于重现的简化示例)。

I had also problems with GroupBoxes and ContentPresenter. I have posted a question and because no answer was given I investigated myself a little. Look at this answer, maybe its the same issue (Additional info: I have not posted my real problem-code but a simplified example that could be used to reproduce).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文