简体   繁体   English

WPF 使单个字符加粗(TreeViewItem)

[英]WPF make single char bold (TreeViewItem)

I have a problem with my C# WPF-Application.我的 C# WPF 应用程序有问题。 For example, I need to make the first letter of the treeViewItem header Bold.例如,我需要将 treeViewItem 标题的第一个字母加粗。 Unfortunately I can't find a solution, does anyone know how exactly I can do this?不幸的是我找不到解决方案,有谁知道我该怎么做?

var treeViewItem = new TreeViewItem
                {
                    IsSelected = false,
                    Padding = new Thickness(105, 0, 105, 0),
                    Margin = new Thickness(0, 3, 0, 0),
                    HorizontalContentAlignment = HorizontalAlignment.Center
                };
                treeViewItem.Header = item.Header.ToString().Split(' ')[0] + _morseCodeAlphabet[i];

You can do this:你可以这样做:

<TreeViewItem>
    <TreeViewItem.Header>
        <TextBlock Loaded="FrameworkElement_OnLoaded" Text="This is a test"/>
    </TreeViewItem.Header>
</TreeViewItem>
private void FrameworkElement_OnLoaded(object sender, RoutedEventArgs e)
{
    if (sender is TextBlock tb)
    {
        string text = tb.Text;
        tb.Text = "";
        int startIndex = 0;
        int endIndex = 1;
        tb.Inlines.Add(new Run(text.Substring(0, startIndex)));
        tb.Inlines.Add(new Bold(new Run(text.Substring(startIndex, endIndex - startIndex))));
        tb.Inlines.Add(new Run(text.Substring(endIndex)));
    }
}

And the result:结果:

在此处输入图像描述

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

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