如何设置WPF扩展器标头的样式?

Vas*_*aga 50 wpf styles header expander

我想在WPF Expander Header上应用一种样式.在下面的XAML中,我有一个Expander,但它的风格不仅适用于标题.

谢谢.

<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="640"
>
    <StackPanel>
        <StackPanel.Resources>
            <Style TargetType="Expander">
                <Style.Resources>
                    <LinearGradientBrush x:Key="BackBrush" StartPoint="0.5,0" EndPoint="0.5,1">
                        <GradientStop Color="#EF3132" Offset="0.1" />
                        <GradientStop Color="#D62B2B" Offset="0.9" />
                    </LinearGradientBrush>
                </Style.Resources>
                <Setter Property="Background" Value="{StaticResource BackBrush}"/>
            </Style>
        </StackPanel.Resources>
        <Expander>
            <StackPanel>
                <TextBlock>Bike</TextBlock>
                <TextBlock>Car</TextBlock>
                <TextBlock>Truck</TextBlock>
            </StackPanel>
        </Expander>
    </StackPanel>
</Page>
Run Code Online (Sandbox Code Playgroud)

Vas*_*aga 53

我结合了Josh SmithMSDN的一些XAML,并提出了一个解决方案.实际上,必须重新控制控制(至少是标题).

<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400">
    <StackPanel>
        <StackPanel.Resources>

            <Style TargetType="Border" x:Key="RacePitBorderStyle" >
                <Style.Resources>
                    <LinearGradientBrush x:Key="BackBrush" StartPoint="0.5,0" EndPoint="0.5,1">
                        <GradientStop Color="#EF3132" Offset="0.1" />
                        <GradientStop Color="#D62B2B" Offset="0.9" />
                    </LinearGradientBrush>
                </Style.Resources>
                <Setter Property="Background" Value="{StaticResource BackBrush}"/>
            </Style>

            <DataTemplate x:Key="titleText">
                <Border Style="{StaticResource RacePitBorderStyle}" Height="24">
                    <TextBlock Text="{Binding}" 
                        Margin="4 0"
                        VerticalAlignment="Center"
                        Foreground="White"
                        FontSize="11" 
                        FontWeight="Normal"
                        Width="{Binding
                        RelativeSource={RelativeSource
                        Mode=FindAncestor,
                        AncestorType={x:Type Expander}},
                        Path=ActualWidth}"
                        TextWrapping="Wrap"/>
                </Border>
            </DataTemplate>

            <Style TargetType="{x:Type Expander}">
                <Setter Property="HeaderTemplate" Value="{StaticResource titleText}"/>
            </Style>

        </StackPanel.Resources>

        <Expander Name="hcontCtrl" Header="This is the header.">
            <StackPanel>
                <TextBox>This is a textbox</TextBox>
                <Button>A button</Button>
            </StackPanel>
        </Expander>

    </StackPanel>
</Page>
Run Code Online (Sandbox Code Playgroud)

  • 我建议删除样式并直接在扩展器上使用'HeaderTemplate'属性.这样可以保留您的默认样式. (3认同)
  • 我发现扩展器宽度是一个真正的问题,你的代码就是这样做的.它完美地在xamlpad中工作.但是在我的代码中它不会导致可怕的图形延迟.Width ="{Binding RelativeSource {RelativeSource Mode = FindAncestor,AncestorType = {x:Type Expander}},Path = ActualWidth}">通过添加上述内容,它可以工作,但是滞后.如果我删除它,或更改路径=宽度,它不起作用,滞后消失. (2认同)

Pat*_*ckV 15

我认为Vasile的答案是在正确的轨道上,但似乎它比原始海报所需要的要多得多.所有原始问题都要求改变标题的背景.虽然所提出的变化确实如此,但它也做了其他事情.

其中一个是用TextBlock替换默认实现,我相信ContentPresenter.那么稍后我们在这个堆叠面板上添加第二个扩展器会发生什么呢?也许是这样的:

<Expander>
    <Expander.Header>
        <StackPanel>
            <Border height="5" width="5" Foreground="Blue"/>
            <TextBlock>Ha!</TextBlock>
        </StackPanel>
    </Expander.Header>
</Expander>
Run Code Online (Sandbox Code Playgroud)

我不知道,但这并不好.相反,我认为我们希望保持这一点.

<DataTemplate x:Key="expanderHeader">
    <ContentPresenter
        Content={Binding}
        TextBlock.Background={StaticResource myBrush}/>
</DataTemplate>

<Style TargetType="Expander">
    <Setter Property="HeaderTemplate" Value="{StaticResource expanderHeader}"/>
</Style>
Run Code Online (Sandbox Code Playgroud)

这样,当有人在我们的样式扩展器中放入不仅仅是文本的东西时,我们就不会破坏.如果你想确保用这个背景包装他们所做的全部内容,这可能是期望的,那将是:

<DataTemplate x:Key="expanderHeader">
    <Border Background={StaticResource myBrush}>
        <ContentPresenter Content={Binding}/>
    </Border>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)


Dom*_*ton 6

取决于你想要的风格 - 你可以设计它的任何部分.如果要更改标题中的内容,只需将所有UI放在Expander.Header属性中,它将显示在标题区域中.

如果这不符合您的需求,您可能需要重新模板控件.在这里查看 WPF中提供的控件模板