C++ UWP:在TreeNode中将文本设置为粗体
我将在C++ UWP:在TreeNode中将文本设置为粗体,c++,text,uwp,treeview,bold,C++,Text,Uwp,Treeview,Bold,我将在C++中的universalwindows平台应用程序中实现TreeView。我已经成功地使用以下代码创建了一个具有一个节点的树状视图: XAML: 现在,我想将文本设置为粗体。我尝试了以下方法: TextBlock ^textBlock = ref new TextBlock(); textBlock->Text = "Hello"; textBlock->FontWeight = Windows::UI::Text::FontWeights::Bold; treeNode-
C++
中的universalwindows平台
应用程序中实现TreeView
。我已经成功地使用以下代码创建了一个具有一个节点的树状视图:
XAML:
现在,我想将文本设置为粗体。我尝试了以下方法:
TextBlock ^textBlock = ref new TextBlock();
textBlock->Text = "Hello";
textBlock->FontWeight = Windows::UI::Text::FontWeights::Bold;
treeNode->Content = textBlock;
treeSolution->RootNodes->Append(treeNode);
代码以粗体显示Windows.UI.Xaml.Controls.TextBlock
而不是Hello
文档中说,在Windows10版本1803中,如果内容不是字符串,则必须重新模板化TreeView控件并指定自定义ItemTemplate。
然后给出了使用音乐和图片库的复杂示例
有人能提供一个简单的例子,说明如何用粗体显示文本吗?谢谢 必须在XAML中为整个控件提供自定义样式,才能设置
TreeViewItemDataTemplate
:
<DataTemplate x:Key="TreeViewItemDataTemplate">
<Grid Height="44">
<TextBlock
Text="{Binding Content}"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Style="{ThemeResource BodyTextBlockStyle}"
FontWeight="Bold" />
</Grid>
</DataTemplate>
<Style TargetType="TreeView">
<Setter Property="IsTabStop" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TreeView">
<TreeViewList x:Name="ListControl"
ItemTemplate="{StaticResource TreeViewItemDataTemplate}"
ItemContainerStyle="{StaticResource TreeViewItemStyle}"
CanDragItems="True"
AllowDrop="True"
CanReorderItems="True">
<TreeViewList.ItemContainerTransitions>
<TransitionCollection>
<ContentThemeTransition />
<ReorderThemeTransition />
<EntranceThemeTransition IsStaggeringEnabled="False" />
</TransitionCollection>
</TreeViewList.ItemContainerTransitions>
</TreeViewList>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
谢谢!对于其他有相同问题的人,我在
中添加了这些代码。我还必须清理项目。
TextBlock ^textBlock = ref new TextBlock();
textBlock->Text = "Hello";
textBlock->FontWeight = Windows::UI::Text::FontWeights::Bold;
treeNode->Content = textBlock;
treeSolution->RootNodes->Append(treeNode);
<DataTemplate x:Key="TreeViewItemDataTemplate">
<Grid Height="44">
<TextBlock
Text="{Binding Content}"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Style="{ThemeResource BodyTextBlockStyle}"
FontWeight="Bold" />
</Grid>
</DataTemplate>
<Style TargetType="TreeView">
<Setter Property="IsTabStop" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TreeView">
<TreeViewList x:Name="ListControl"
ItemTemplate="{StaticResource TreeViewItemDataTemplate}"
ItemContainerStyle="{StaticResource TreeViewItemStyle}"
CanDragItems="True"
AllowDrop="True"
CanReorderItems="True">
<TreeViewList.ItemContainerTransitions>
<TransitionCollection>
<ContentThemeTransition />
<ReorderThemeTransition />
<EntranceThemeTransition IsStaggeringEnabled="False" />
</TransitionCollection>
</TreeViewList.ItemContainerTransitions>
</TreeViewList>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>