[英]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
上的SelectedItem
或SelectedValue
设置为控件的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.