如何在WPF中显示选定的节点自定义Treeview [英] how to show selected node custom treeview in wpf

查看:232
本文介绍了如何在WPF中显示选定的节点自定义Treeview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在wpf中具有自定义树视图


I have custom treeview in wpf


<treeview grid.row="0" grid.column="0" name="ApplicationTree" datacontext="{Binding}" itemssource="{Binding Master}" itemtemplate="{StaticResource MasterTemplate}" borderbrush="Black" borderthickness="3,3,3,3" height="500">

<datatemplate x:key="DetailTemplate" xmlns:x="#unknown">
            <canvas height="30">
                <Image Source="{Binding Imageurl}" Width="25" Height="25" VerticalAlignment="Center"/>
                <Label Name="lblDetailTree"  Content="{Binding Info}" FontSize="15" Canvas.Top="0" Canvas.Left="30" MouseDown="lblDetailTree_MouseDown" MouseUp="lblDetailTree_MouseUp" />
            </canvas>
        </datatemplate>

private void lblDetailTree_MouseUp(object sender, MouseButtonEventArgs e)
        {
            System.Windows.Controls.Label lblnode = (System.Windows.Controls.Label)sender;
            lblnode.Background = Brushes.CadetBlue;
        }


</treeview>



当我选择节点时,我得到了选择的节点标签,然后我更改了选择的节点的颜色&它的变化..但是当我选择另一个节点时,我想将先前选择的节点的颜色更改回其旧样式.



when i select node, i get selected node label,then i change color of selected node & its changing..but when i selected another node then i want to change color of previously selected node back to its old style.

need help asap..thanks in advance.

推荐答案

好问题,因为此技术非常重要,因为用作树节点的框架元素的默认着色非常难看,因此在很多情况下您都需要这样的代码.

假设您将TextBlock用于可选项目,还假定TextBlock具有默认的黑白颜色(或在添加新项目时以编程方式设置所有这些颜色,使其与selected/未选择的颜色请在下面使用):

Good question, because this technique is very important, because default coloring of framework elements used as tree nodes is quite ugly, so you need such code in many cases.

Let''s assume you use TextBlock for your selectable items, also assume that the TextBlock has default black-on-white colors (or set all those colors programmatically when a new item is added to be in sync with selected/deselected colors use below):

lblDetailTree.SelectedItemChanged += (sender, eventArgs) => {
    TextBlock oldItem = eventArgs.OldValue as TextBlock;
    TextBlock newItem = eventArgs.NewValue as TextBlock;
    if (oldItem != null) {
        oldItem.Background = Brushes.White;
        oldItem.Foreground = Brushes.Black;
    } //if oldItem
    if (newItem != null) {
        newItem.Background = Brushes.CadetBlue;
        newItem.Foreground = Brushes.Yellow;
    } //if newItem
}; //lblDetailTree.SelectedItemChanged



经过测试:CadetBlue看起来不太好,请尝试Navy :-).

您不能将字符串用作节点(此对象没有内容可绘制:-)).由于WPF的内容模型非常灵活,因此要为对节点类型不敏感的多态对象或通用代码编写此效果有点困难.例如,不可能使用直接基类TextBlock,因为这是FrameworkElement,它没有BackgroundForeground属性,因此,它使用控件和其他框架元素的组合,对于每个不是Control的基于FrameworkElement的类,将需要进行单独的强制转换/检查,而对于所有基于Control的类,则需要进行单独的强制转换/检查.

—SA



Tested: CadetBlue does not look so nice, try Navy :-).

You cannot use strings as nodes (there is nothing to paint in this object :-)). Because of a very flexible content model of WPF, writing this effect for polymorphous objects or universal code insensitive to the node type is a bit difficult. For example, using direct base class of TextBlock is not possible, because this is FrameworkElement, which does not have Background or Foreground property, so, it you use a combination of controls and other framework elements, you will need to make a separate cast/check for every FrameworkElement-based class which is not Control and one for all Control-based classes.

—SA


这篇关于如何在WPF中显示选定的节点自定义Treeview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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