WPF:单个TreeViewItem粗体

时间:2018-07-18 04:55:21

标签: wpf vb.net xaml

我是Xaml的新手,因此我被卡在这里。我用谷歌搜索了许多不同的方法,但是都没有用:-(我希望有人在这里有个主意,可以为我提供帮助:-)

借助互联网上的一些教程,我得到了这个TreeView。它像一个咒语一样工作:-)

<TreeView>                    
    <TreeViewItem ItemsSource="{Binding Data.Number}">                    
        <TreeViewItem.Header>
            <StackPanel Orientation="Horizontal">
                <Image Width="15" Margin="5 0 5 0"  Source="image.ico"/>
                <TextBlock Text="{Binding Data.ID}"/>
                <TextBlock Text=" [" Foreground="Blue"/>
                <TextBlock Text="{Binding Data.Number.Count}" Foreground="Blue"/>
                <TextBlock Text="]" Foreground="Blue"/>
            </StackPanel>
        </TreeViewItem.Header>
        <TreeViewItem.ItemTemplate>
            <DataTemplate>
                <TreeViewItem Header="{Binding ItemID}">
                    <TreeViewItem ItemsSource="{Binding ItemInfo}">
                        <TreeViewItem.Header>
                            <StackPanel Orientation="Horizontal">
                                <Image Width="15" Margin="5 0 5 0"  Source="image.ico"/>
                            <TextBlock Text="Items"/>
                            <TextBlock Text=" [" Foreground="Blue"/>
                                <TextBlock Text="{Binding ItemInfo.Count}" Foreground="Blue"/>
                                <TextBlock Text="]" Foreground="Blue"/>
                            </StackPanel>
                        </TreeViewItem.Header>
                        <TreeViewItem.ItemTemplate>
                            <DataTemplate>
                            <TextBlock Text="{Binding Value.Number}"/>
                        </DataTemplate>
                        </TreeViewItem.ItemTemplate>
                    </TreeViewItem>
                    <TreeViewItem ItemsSource="{Binding SetInfo}">
                        <TreeViewItem.Header>
                            <StackPanel Orientation="Horizontal">
                                <Image Width="15" Margin="5 0 5 0"  Source="image.ico"/>
                                <TextBlock Text="Sets"/>
                                <TextBlock Text=" [" Foreground="Blue"/>
                                <TextBlock Text="{Binding SetInfo.Count}" Foreground="Blue"/>
                                <TextBlock Text="]" Foreground="Blue"/>
                            </StackPanel>
                        </TreeViewItem.Header>
                        <TreeViewItem.ItemTemplate>
                            <DataTemplate>
                            <TextBlock Text="{Binding Value.ID}"/>
                             </DataTemplate>
                        </TreeViewItem.ItemTemplate>
                    </TreeViewItem>
                    <TreeViewItem ItemsSource="{Binding GroupInfo}">
                        <TreeViewItem.Header>
                            <StackPanel Orientation="Horizontal">
                                <Image Width="15" Margin="5 0 5 0"  Source="image.ico"/>
                                <TextBlock Text="Groups"/>
                                <TextBlock Text=" [" Foreground="Blue"/>
                                <TextBlock Text="{Binding GroupInfo.Count}" Foreground="Blue"/>
                                <TextBlock Text="]" Foreground="Blue"/>
                            </StackPanel>
                        </TreeViewItem.Header>
                        <TreeViewItem.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Value.Number}"/>
                            </DataTemplate>
                        </TreeViewItem.ItemTemplate>
                    </TreeViewItem>
                </TreeViewItem>
            </DataTemplate>
        </TreeViewItem.ItemTemplate>
    </TreeViewItem>                    
</TreeView>

现在的问题是我希望所选项目(单击鼠标)变粗体。但仅选中的项目(不包括下面或上面的项目)。物品在哪个级别上都没有关系。

我尝试了很多变化,但是没有一个起作用。这两个是最好的,但是他们总是选择一个孔节点或只是顶部节点,我无法将其分解为一个节点。

<TreeView.ItemContainerStyle>
    <Style TargetType="{x:Type TreeViewItem}">
        <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
        <Setter Property="FontWeight" Value="Normal"/>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="FontWeight" Value="Bold"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</TreeView.ItemContainerStyle>

<TextBlock.Style>
    <Style>
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TreeViewItem}}, FallbackValue=False}" Value="True">
                <Setter Property="TextBlock.FontWeight" Value="Bold" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</TextBlock.Style>

1 个答案:

答案 0 :(得分:0)

您可以尝试这个Style吗?

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