简体   繁体   English

UWP:在TreeNode中将文本设置为粗体

[英]UWP: Set text to bold in TreeNode

I am following Microsoft's documentation to implement a TreeView in a Universal Windows Platform app in C++ . 我正在按照Microsoft的文档C++中的Universal Windows Platform应用程序中实现TreeView I have successfully been able to create a tree view with one node using the following codes: 我已经能够使用以下代码成功创建一个节点的树视图:

XAML: XAML:

<TreeView x:Name="treeSolution"></TreeView>

C++: C ++:

TreeViewNode ^treeNode = ref new TreeViewNode();
treeNode->Content = "Hello";
treeSolution->RootNodes->Append(treeNode);

Now, I want to set the text to bold. 现在,我想将文本设置为粗体。 I tried the following: 我尝试了以下方法:

TextBlock ^textBlock = ref new TextBlock();
textBlock->Text = "Hello";
textBlock->FontWeight = Windows::UI::Text::FontWeights::Bold;
treeNode->Content = textBlock;
treeSolution->RootNodes->Append(treeNode);

The code displays Windows.UI.Xaml.Controls.TextBlock instead of Hello in bold. 该代码以粗体显示Windows.UI.Xaml.Controls.TextBlock而不是Hello

The documentation says that In Windows 10, version 1803, you have to retemplate the TreeView control and specify a custom ItemTemplate if your content is not a string. 该文档说, In Windows 10, version 1803, you have to retemplate the TreeView control and specify a custom ItemTemplate if your content is not a string. It then gives a complex example using the Music and Picture library. 然后使用音乐和图片库给出了一个复杂的示例。

Could somebody provide a simple example of how to display the text in bold? 有人可以提供一个简单的示例以粗体显示文本吗? Thanks. 谢谢。

You have to provide a custom style for the whole control in XAML to be able to set the TreeViewItemDataTemplate : 您必须为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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM