Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将扩展器控制标题设置为粗体_C#_Wpf_Expander - Fatal编程技术网

C# 将扩展器控制标题设置为粗体

C# 将扩展器控制标题设置为粗体,c#,wpf,expander,C#,Wpf,Expander,我将在运行时动态创建n个expander控件,如下所示 theExpanderCaption[counter] = new TextBlock(); theExpanderCaption[counter].FontWeight = FontWeights.Bold; theExpanderCaption[counter].Text = ITEMIMP.DataConstants.GroupIds.GetGroupCaption(group.GroupId); theGroupExpander[

我将在运行时动态创建n个expander控件,如下所示

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;

在上面的代码中,我使用文本块数组来设置expander头(使其加粗)。这里的缺点是我必须使用nno文本块控件。有没有其他方法可以实现这一点?

查看您的标记会有所帮助,但我假设您有这样的方法:

<Expander Header="My Header">
    ...
</Expander>

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

<Expander>
    <Expander.Header>
        <TextBlock Text="My Header" FontWeight="Bold" />
    </Expander.Header>
    ...
</Expander>

...

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

        <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>


您可以在运行时创建FormattedText并设置为Header。在FormattedText的构造函数中,您可以定义前景笔刷。

详细说明您的问题!如何在运行时实现这一点,如果动态创建expander控件(在不使用文本块的情况下设置标头)没有触发器,默认情况下,is expanded属性设置为trueOne more detail,如果只对文本的一部分进行格式化,这可能会有所帮助:
粗体文本正常,但红色文本