UWP: установить жирный текст в TreeNode

Я следую Документация Microsoft реализовать TreeView в Universal Windows Platform приложение в C++, Я успешно смог создать представление дерева с одним узлом, используя следующие коды:

XAML:

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

C ++:

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

Теперь я хочу сделать текст жирным. Я попробовал следующее:

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 жирным шрифтом.

В документации сказано, что In Windows 10, version 1803, you have to retemplate the TreeView control and specify a custom ItemTemplate if your content is not a string. Затем приводится сложный пример с использованием библиотеки музыки и изображений.

Может ли кто-нибудь привести простой пример того, как отобразить текст жирным шрифтом? Благодарю.

0

Решение

Вы должны предоставить собственный стиль для всего элемента управления в 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>
0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector