6

xaml 上的树视图代码

<Window x:Class="WpfApplication1.orderdetail" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="Window1" Height="250" Width="450">

<Window.Resources>
    <HierarchicalDataTemplate x:Key="NodeTemplate">
        <TextBlock x:Name="text" Text="?" />
        <HierarchicalDataTemplate.ItemsSource>
            <Binding XPath="child::node()" />
        </HierarchicalDataTemplate.ItemsSource>
        <HierarchicalDataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=NodeType}" Value="Text">
                <Setter TargetName="text" Property="Text" Value="{Binding Path=Value}"></Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
                <Setter TargetName="text" Property="Text" Value="{Binding Path=Name}"></Setter>
            </DataTrigger>
        </HierarchicalDataTemplate.Triggers>
    </HierarchicalDataTemplate>
    <XmlDataProvider x:Key="xmlDataProvider"></XmlDataProvider>
</Window.Resources>

<Grid >
    <TreeView Name="treeView1"
          Background="AliceBlue"
          ItemsSource="{Binding Source={StaticResource xmlDataProvider}, XPath=*}"
          ItemTemplate= "{StaticResource NodeTemplate}"/>
</Grid>
</Window>

.cs 文件

 public partial class orderdetail : Window
{
    public int OID { get; set; }
    public orderdetail()
    {
        InitializeComponent();
    }

    private void Bindtree(int orderid)
    {
        string xml = "xml content will be here from api method";
        XmlDataProvider dataProvider = this.FindResource("xmlDataProvider") as XmlDataProvider;
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xml);
        dataProvider.Document = doc;

    }
}

我不想将树视图与窗口 InitializeComponent 事件绑定,如下所示

    public orderdetail()
    {
        InitializeComponent();
        Bindtree(1);
    }

我将动态地将值传递给此页面以绑定树视图,如下所示

 orderdetail orderdetail = new orderdetail();
 orderdetail.OID = Convert.ToInt32(1);
 orderdetail.Show();

是否有任何事件/方法可以动态绑定树视图?

4

2 回答 2

10

使用 WPF,您应该利用 MVVM 模式。我看过一个非常好的使用TreeViewMVVM 的教程:http: //www.codeproject.com/Articles/26288/Simplifying-the-WPF-TreeView-by-Using-the-ViewMode

于 2013-09-17T06:39:28.167 回答
0

好模板 <Grid> <TreeView x:Name="treeView" HorizontalAlignment="Left" Height="227" Margin="10,32,0,0" VerticalAlignment="Top" Width="178" /> </Grid>

和 C#

 static TreeViewItem item;
    static public void Show(TreeView tree)
    {

        DataSet dsSet = Class.GetDS();

        for (int i = 0; dsSet.Tables[0].Rows.Count > i; i++)
        {
            item = new TreeViewItem();
            item.Header = dsSet.Tables[0].Rows[i][1];

            for (int j = 0; j < 2; j++)
            {
                item.Items.Add("Monitor");
                item.Items.Add("LapTop");
            }
            tree.Items.Add(item);
        }
    }
Show(treeView);

类.GetDS(); 是我从另一个类中获取的静态方法,您可以添加任何额外的 DataSet。

于 2016-04-16T13:16:09.620 回答