简体   繁体   中英

WPF make single char bold (TreeViewItem)

I have a problem with my C# WPF-Application. For example, I need to make the first letter of the treeViewItem header Bold. 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:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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