向树视图添加按钮 [英] Add button to treeview

查看:88
本文介绍了向树视图添加按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法在WPF中的TreeView中为所选项添加按钮,但是没有找到任何有用的解决方案。



我正在导入来自XML文件的数据:



我尝试过:



 XmlDocument doc = recources.Xml_load(); 

foreach (XmlNode n doc.SelectNodes( / LMSProjekt / Projekt))
{
XmlNode n_name = n.SelectSingleNode( 名称);
TreeViewItem itm = new TreeViewItem();
// itm.Items.Add(cb);
itm.Header = n_name.InnerText;
Treeview.Items.Add(itm);

foreach (XmlNode n_desc in n.SelectNodes( Prozess))
{
n_name = n_desc.SelectSingleNode( ProzessName);
TreeViewItem sub_itm = new TreeViewItem();
sub_itm.Header = n_name.InnerText; // 我想在此处添加一个按钮
itm.Items.Add(sub_itm) ;

foreach (XmlNode n_desc2 in n_desc.SelectNodes( description))
{
TreeViewItem sub2_itm = TreeViewItem();
sub2_itm.Header = n_desc2.InnerText;
sub_itm.Items.Add(sub2_itm);
selected = n_desc2.InnerText;
}
}
}

解决方案

嗨会员,



看一下这个想法:



 <  窗口  

xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation

xmlns:x < span class =code-keyword> = http://schemas.microsoft.com/winfx/2006/xaml

xmlns:d = http://schemas.microsoft.com/expression/blend/2008

xmlns:mc = http://schemas.openxmlformats.org/markup-compatibility/2006

xmlns:local = clr -namespace:WpfApplication2

xmlns:System = clr-namespace:System; assembly = mscorlib

x:Class = WpfApplication2.MainWindow

mc:Ignorable = d

< span class =code-attribute>标题 = MainWindow

高度 = 350

宽度 = 525 >

< Window.Resources >
<! - 因为我们需要将可见性枚举值转换为布尔值(TreeViewItem的IsSelected属性),我们需要一个转换器 - 让我们使用默认值,我们用它创建一个窗口级资源 - >
< BooleanToVisibilityConverter

x:Key = BooleanToVisibilityConverter >
< / BooleanToVisibilityConverter >
< / Window.Resources >
< 网格 >

< TreeView >
<! - 定义您希望树节点的ItemTemplate(可以通过为DataTemplate设置DataType,为不同类型的节点使用不同的节点) - >
< TreeView.ItemTemplate >
< DataTemplate >

<! - 例如,我们使用TextBlock和水平堆叠的Button的组合(添加边距等等以获得更好的外观) - >
< StackPanel

< span class =code-attribute>
方向 = 水平 >
< TextBlock

< span class =code-attribute> 文本 = {Binding} / >
<! - 这是诀窍:每个添加的项目(在本例中只是字符串)将由Treeview包装在TreeViewItem中,因此我们绑定按钮
对其上层TreeViewItem的IsSelected属性的可见性,然后我们使用我们之前定义的转换器将布尔值转换为有意义的可见性枚举值
- >

< 按钮 < span class =code-attribute>

< span class =code-attribute> 内容 = clickme

< span class =code-attribute> 可见性 = {Binding Path = IsSelected,RelativeSource = {RelativeSource Mode = FindAncestor,AncestorType = {x:Type TreeViewItem}},Converter = {StaticResource BooleanToVisibilityConverter} } >
< /按钮 >
< / StackPanel >
< / DataTemplate >
< / TreeView.ItemTemplate >

< TreeViewItem > < / TreeViewItem >

<! - 只是一些示例项目(实际上您很可能使用ItemsSource属性来绑定数据 - >
< 系统:字符串 > bla < / System:String >
< 系统:字符串 > bla < / System:String >
< span class =code-keyword>< 系统:字符串 > bla < span class =code-keyword>< / System:String >
< 系统:字符串 > bla < / System:String >
< 系统:字符串 > bla < / System:String >


< / TreeView >

< span class =code-keyword>< / Grid >
< / Window >


I'm looking for a way to add button to a selected item in TreeView in WPF, but haven't found any useful solutions.

I am importing data from an XML file:

What I have tried:

XmlDocument doc = recources.Xml_load();

            foreach (XmlNode n in doc.SelectNodes("/LMSProjekt/Projekt"))
            {
                XmlNode n_name = n.SelectSingleNode("Name");
                TreeViewItem itm = new TreeViewItem();
                //itm.Items.Add(cb);
                itm.Header = n_name.InnerText;
                Treeview.Items.Add(itm);

                foreach (XmlNode n_desc in n.SelectNodes("Prozess"))
                {
                    n_name = n_desc.SelectSingleNode("ProzessName");
                    TreeViewItem sub_itm = new TreeViewItem();
       sub_itm.Header = n_name.InnerText; //i would like to add a button HERE 
                    itm.Items.Add(sub_itm);

                    foreach (XmlNode n_desc2 in n_desc.SelectNodes("description"))
                    {   
                        TreeViewItem sub2_itm = new TreeViewItem();
                        sub2_itm.Header = n_desc2.InnerText ;
                        sub_itm.Items.Add(sub2_itm);
                        selected = n_desc2.InnerText;
                    }  
                }
            }

解决方案

Hi Member,

Have a look at this to get the idea:

<Window

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    xmlns:local="clr-namespace:WpfApplication2"

    xmlns:System="clr-namespace:System;assembly=mscorlib"

    x:Class="WpfApplication2.MainWindow"

    mc:Ignorable="d"

    Title="MainWindow"

    Height="350"

    Width="525">

    <Window.Resources>
        <!--Because we need to convert the visibility enum value to a boolean (the IsSelected property of the TreeViewItem) we need a converter - let's use the default one, we create a window-level resource out of it-->
        <BooleanToVisibilityConverter

            x:Key="BooleanToVisibilityConverter">
        </BooleanToVisibilityConverter>
    </Window.Resources>
    <Grid>

        <TreeView>
            <!--Define an ItemTemplate like you wish for you tree nodes (could use different ones, for different kind of nodes by setting a DataType for the DataTemplate)-->
            <TreeView.ItemTemplate>
                <DataTemplate>

                    <!--As example we use a combination of a TextBlock and a Button stacked horizontally (add margins and so on to get a nicer look)-->
                    <StackPanel

                        Orientation="Horizontal">
                        <TextBlock

                            Text="{Binding}" />
                        <!--here is the trick: Every added item (in this example just strings) will be wrapped in a TreeViewItem by the Treeview, so we bind the visibility of the button
                            to the IsSelected property of its "upper" TreeViewItem, then we use the converter we defined earlier to convert the boolean value to a meaningful Visibility enum value -->
                        <Button

                            Content="clickme"

                            Visibility="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}, Converter={StaticResource BooleanToVisibilityConverter}}">
                        </Button>
                    </StackPanel>
                </DataTemplate>
            </TreeView.ItemTemplate>

            <TreeViewItem></TreeViewItem>

            <!--Just some sample items (in real you will most likely use ItemsSource property to bind data-->
            <System:String>bla</System:String>
            <System:String>bla</System:String>
            <System:String>bla</System:String>
            <System:String>bla</System:String>
            <System:String>bla</System:String>


        </TreeView>

    </Grid>
</Window>


这篇关于向树视图添加按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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