我将在运行时动态创建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 文本块控件.有没有其他方法来实现这一目标?
查看您的标记会有所帮助,但我假设您有以下内容:
<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)
在窗口/页面资源中添加以下内容.这将应用于窗口内的所有扩展器
<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)
| 归档时间: |
|
| 查看次数: |
1426 次 |
| 最近记录: |