你如何突出树型视图? [英] How do you highlight TreeViewItem?

查看:112
本文介绍了你如何突出树型视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有所有的默认颜色简单的树形。当你点击一个项目被突出显示和背景变为蓝色。精细至今。我试图做同样的事情在后面的代码,这样我可以有一个TreeView项的我的选择以蓝色突出显示。我看到,我已经发现,改变背景的属性是.IsSelected。但是,当我设置为true,TreeView项以白色高亮显示。我不明白这一点。我不高亮颜色设置为任何东西所以它为什么会选择不同的高亮颜色当您选择用鼠标相同的项目点击比?是突出一个不同的属性不是选择,如果因此如何那是什么属性的名称?



感谢



编辑:添加代码(我的道歉,这是如此简单,所以我没想到任何人都希望看到的代码)



XAML中:。(是的,这整个代码只需插入一个项目,它会运行)

 <窗口x:类=TestTreeView.Window1
的xmlns =http://schemas.microsoft.com/ WinFX的/ 2006 / XAML /演示
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
标题=树视图图标
ShowInTaskbar = 假
NAME =MyTestTreeView
背景=米色
WIDTH =500
HEIGHT =500
>
< StackPanel的名称=StackPanel的>
<按钮名称=ADDNODE点击=btnClick>
ADDNODE
< /按钮>

<的ScrollViewer Horizo​​ntalScrollBarVisibility =自动VerticalScrollBarVisibility =隐藏名称=的ScrollViewer>
<树视图名称=树视图了borderThickness =0>
< TreeView.Resources>
<的SolidColorBrush颜色=红色X:键={X:静态SystemColors.HighlightBrushKey}/>
< /TreeView.Resources>
< / TreeView的>
< /&的ScrollViewer GT;
< / StackPanel的>





后面的代码:

 使用System.Windows.Shapes; 
:使用System.IO;
命名空间TestTreeView
{
公共部分类窗口1:System.Windows.Window
{
公共窗口1()
{
的InitializeComponent() ;
}
公共无效btnClick(对象发件人,RoutedEventArgs E)
{
树型视图N1 =新的TreeViewItem();
n1.Header =顶级节点;
n1.IsSelected = TRUE;
n1.Focus();
treeView.Items.Add(N1);
}
}
}


解决方案

TreeviewItem.Focus()方法可以解决你的问题。在你的代码已经错过了设置ItemContainerStyle,你有以下



添加到TreeView控件等之后集中树型视图

 <窗口x :类=WpfApplication3.MainWindow
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =http://schemas.microsoft。 COM / WinFX的/ 2006 / XAML
标题=主窗口HEIGHT =350WIDTH =525>
< Window.Resources>
<风格X:键=myLBStyle的TargetType ={X:类型的TreeViewItem}>
< Style.Resources>
<的SolidColorBrush X:键={X:静态SystemColors.HighlightBrushKey}颜色=绿色/>
<的SolidColorBrush X:键={X:静态SystemColors.HighlightTextBrushKey}颜色=红色/>
< /Style.Resources>
< /样式和GT;
< /Window.Resources>
<网格和GT;
<树视图名称=为TreeView1ItemContainerStyle ={StaticResource的myLBStyle}/>
<按钮内容=选择WIDTH =300HEIGHT =30点击=Button_Click_2/>
< /网格和GT;





 树型视图N1 =新的TreeViewItem(); 
n1.Header =顶级节点;
n1.IsSelected = TRUE;

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


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". But when I set that to true the treeview item is highlighted in white. 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.)

 <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. In your code you have missed to set ItemContainerStyle and you have to focus TreeViewItem after adding into the TreeView like below

<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();

这篇关于你如何突出树型视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆