当前位置: 动力学知识库 > 问答 > 编程问答 >

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: