简体   繁体   English

如何突出显示TreeViewItem?

[英]How do you highlight TreeViewItem?

I have a simple treeview with all the default colors. 我有一个带有所有默认颜色的简单树形视图。 When you click on an item it is highlighted and the background turns blue. 当您单击某个项目时,它将突出显示,并且背景变为蓝色。 Fine so far. 到目前为止很好。 I'm trying to do the same thing in the code behind so that I can have a treeview item of my choice highlighted in blue. 我正在尝试在后面的代码中执行相同的操作,以便我可以选择以蓝色突出显示的树形视图项目。 The only property I see that I have found that changes the background is ".IsSelected". 我发现发现改变背景的唯一属性是“ .IsSelected”。 But when I set that to true the treeview item is highlighted in white. 但是,当我将其设置为true时,treeview项目将以白色突出显示。 I don't get it. 我不明白 I'm not setting the highlight color to anything so why would it pick a different highlight color than when you select that same item with a mouse click? 我没有将突出显示颜色设置为任何颜色,因此为什么与用鼠标单击选择相同项目时会选择不同的突出显示颜色? Is "highlight" a different property than "select" and if so how what is the name of that property? “突出显示”与“选择”属性是否不同?如果是,该属性的名称是什么?

Thanks. 谢谢。

Edit: Adding code (My apologies, this was so simple so I didn't think anyone would want to see the code) 编辑:添加代码(我很抱歉,这是如此简单,所以我认为没有人愿意看到该代码)

Xaml: (and yes, this the entire code. Just plug it into a project and it'll run.) Xaml :(是的,这是完整的代码。只需将其插入项目中即可运行。)

 <Window x:Class="TestTreeView.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Tree View Icon"
ShowInTaskbar="false"
Name ="MyTestTreeView"
Background="Beige"
Width="500"
Height ="500"
>
<StackPanel Name="stackpanel">
    <Button Name="AddNode" Click="btnClick">
            AddNode
    </Button>

    <ScrollViewer HorizontalScrollBarVisibility="auto" VerticalScrollBarVisibility="hidden" Name="scrollViewer">
        <TreeView Name="treeView" BorderThickness="0">
            <TreeView.Resources>
                <SolidColorBrush Color="Red" x:Key="{x:Static SystemColors.HighlightBrushKey}"/>
            </TreeView.Resources>
        </TreeView>
    </ScrollViewer>
</StackPanel>

Code behind: 后面的代码:

using System.Windows.Shapes;
using System.IO;
namespace TestTreeView
{
    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            InitializeComponent();
        }       
        public void btnClick(object sender, RoutedEventArgs e)
        {
            TreeViewItem n1 = new TreeViewItem();
            n1.Header = "Top Node";
            n1.IsSelected = true;
            n1.Focus();
            treeView.Items.Add(n1);          
        }
    }
}

TreeviewItem.Focus() method would solve your problem. TreeviewItem.Focus()方法将解决您的问题。 In your code you have missed to set ItemContainerStyle and you have to focus TreeViewItem after adding into the TreeView like below 在您的代码中,您错过了设置ItemContainerStyle的麻烦,并且在将TreeViewItem添加到如下所示的TreeViewItem之后,必须将其聚焦

<Window x:Class="WpfApplication3.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 x:Key="myLBStyle" TargetType="{x:Type TreeViewItem}">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Green" />
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Red" />
        </Style.Resources>
    </Style>
</Window.Resources>
<Grid>
    <TreeView Name="Treeview1" ItemContainerStyle="{StaticResource myLBStyle}" />
    <Button Content="Select" Width="300" Height="30" Click="Button_Click_2" />
</Grid>

TreeViewItem n1 = new TreeViewItem();
n1.Header = "Top Node";
n1.IsSelected = true;

Treeview1.Items.Add(n1);
n1.Focus();

使用TreeViewItem的Focus()方法。

I believe the most maintainable approach would require you to set the SelectedItem or SelectedValue on your TreeView to be one of the items of interest in your ItemsSource for the control. 我认为最可维护的方法将要求您将TreeView上的SelectedItemSelectedValue设置为控件的ItemsSource中感兴趣的项之一。 If you are using bindings, this should be very easy to do -- just update the respective property on your ViewModel. 如果您正在使用绑定,则这应该非常容易-只需更新ViewModel上的相应属性即可。

This is what you need to do if you are trying to mimic the user picking an item from your list. 如果您试图模仿用户从列表中选择一个项目,这就是您需要做的。 Let me know if I am incorrect or if you need more guidance, but we will need more code/examples from you first most likely. 让我知道我是否不正确,或者您是否需要更多指导,但是我们很可能会首先从您那里获取更多代码/示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在使用ControlTemplate时,如何重现TreeViewItem中显示的文本? - How do you reproduce the text displayed in a TreeViewItem while using a ControlTemplate? 如何存储TreeViewItem ToggleButton,类似于TreeViewItem标头对象? - How do I store TreeViewItem ToggleButton, similar to TreeViewItem Header object? 仅在具有图像的TreeViewItem中突出显示TextBlock - Highlight TextBlock only in TreeViewItem with Image 如何在wpf中的treeviewitem中访问treeviewitem? - How to access a treeviewitem with in a treeviewitem in wpf? 如何使用 WPF 突出显示 combobox 中的项目? - How do you highlight an item in a combobox using WPF? 如何在Gtk Sharp中创建语法高亮文本编辑器? - How do you make a Syntax Highlight Text Editor in Gtk Sharp? 如何在富文本框中搜索字符串并突出显示所有找到的内容或突出显示每一行? - How do you search for a string in a rich text box and highlight all found or highlight each line? 在Visual Studio中仅突出显示一个对象时,如何自动突出显示所有具有相同名称的对象? - How do you automatically highlight all objects with same name in Javascript, when you highlight only one in Visual Studio? 如何获取所选TreeViewItem的文本值? - How do I get the text value of the selected TreeViewItem? 如何从 TReeViewItem 上下文菜单访问 TreeView COntext? - How do I access the TreeView COntext from the TReeViewItem Context Menu?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM