Home > Blockchain >  WPF make single char bold (TreeViewItem)
WPF make single char bold (TreeViewItem)

Time:05-23

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];

CodePudding user response:

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:

enter image description here

  • Related