简体   繁体   中英

How do you make one line bold in a TreeView Class in WPF/C#?

How do you make one line bold in a TreeView Class in WPF/C#?

I mean, how do you make just one line in a Tree View bold at runtime?

I think you can do this with the help of the styles and triggers. for example in this sample code the selected item will become bold and also its size will be changed to 16 from 12.

<Window x:Class="WpfApplication17.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" >

    <Window.Resources>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="FontWeight" Value="Normal"></Setter>
            <Setter Property="FontSize" Value="12"></Setter>
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="FontWeight" Value="Bold"></Setter>
                    <Setter Property="FontSize" Value="16"></Setter>
                </Trigger>          
            </Style.Triggers>      
        </Style>

    </Window.Resources>
    <Grid>
        <TreeView>
            <TreeViewItem Header="Computer1">
                <TreeViewItem Header="UserGroup1">
                    <TreeViewItem Header="User1"></TreeViewItem>
                    <TreeViewItem Header="User2"></TreeViewItem>
                    <TreeViewItem Header="User3"></TreeViewItem>
                </TreeViewItem>
                <TreeViewItem Header="UserGroup2">
                    <TreeViewItem Header="User4"></TreeViewItem>
                    <TreeViewItem Header="User5"></TreeViewItem>
                    <TreeViewItem Header="User6"></TreeViewItem>
                </TreeViewItem>
            </TreeViewItem>

        </TreeView>
    </Grid>
</Window>

you can also change the Font Weight of a single item by using the "FontWeight" property of the TreeViewItem object that you want to make it bold like as I did in the following code. in this code only the UserGroup1 TreeViewItem is bold.

<Grid>
    <TreeView>
        <TreeViewItem Header="Computer1">
            <TreeViewItem Header="UserGroup1" FontWeight="Bold">
                <TreeViewItem Header="User1"></TreeViewItem>
                <TreeViewItem Header="User2"></TreeViewItem>
                <TreeViewItem Header="User3"></TreeViewItem>
            </TreeViewItem>
            <TreeViewItem Header="UserGroup2">
                <TreeViewItem Header="User4"></TreeViewItem>
                <TreeViewItem Header="User5"></TreeViewItem>
                <TreeViewItem Header="User6"></TreeViewItem>
            </TreeViewItem>
        </TreeViewItem>

    </TreeView>
</Grid>

I hope this will be your answer.

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