使扩展器控件标头变为粗体

Ste*_*phy 0 c# wpf expander

我将在运行时动态创建n扩展器控件,如下所示,

theExpanderCaption[counter] = new TextBlock();
theExpanderCaption[counter].FontWeight = FontWeights.Bold;
theExpanderCaption[counter].Text = ITEMIMP.DataConstants.GroupIds.GetGroupCaption(group.GroupId);

theGroupExpander[counter] = new Expander();
theGroupExpander[counter].Header = theExpanderCaption[counter];
theGroupExpander[counter].Margin = new Thickness(10);
theGroupExpander[counter].HorizontalAlignment = HorizontalAlignment.Left;
theGroupExpander[counter].IsExpanded = true;
theGroupExpander[counter].Content = this.theGroupGrids[counter];
theGroupExpander[counter].Style = null;
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我使用文本块数组来设置扩展器头(使其变为粗体).这里的缺点是我必须使用n no 文本块控件.有没有其他方法来实现这一目标?

Gra*_*ICA 6

查看您的标记会有所帮助,但我假设您有以下内容:

<Expander Header="My Header">
    ...
</Expander>
Run Code Online (Sandbox Code Playgroud)

您可以像这样指定标题,并设置您想要的所有格式选项:

<Expander>
    <Expander.Header>
        <TextBlock Text="My Header" FontWeight="Bold" />
    </Expander.Header>
    ...
</Expander>
Run Code Online (Sandbox Code Playgroud)


Nit*_*tin 5

在窗口/页面资源中添加以下内容.这将应用于窗口内的所有扩展器

        <DataTemplate x:Key="HeaderTemplate">
               <TextBlock Text="{Binding}" 
                    VerticalAlignment="Center"
                    FontWeight="Bold"
                    Width="{Binding
                    RelativeSource={RelativeSource
                    Mode=FindAncestor,
                    AncestorType={x:Type Expander}},
                    Path=ActualWidth}"
                    TextWrapping="Wrap"/>
        </DataTemplate>

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