C# 如何在 WPF 中动态填充树视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18842620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fill treeview in WPF dynamically
提问by Chhatrapati Sharma
Treeview code on xaml
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 file
.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;
}
}
I don't want to bind treeview with window InitializeComponent event as like below
我不想将树视图与窗口 InitializeComponent 事件绑定,如下所示
public orderdetail()
{
InitializeComponent();
Bindtree(1);
}
I will pass dynamically value to this page for bind treeview like below
我将动态值传递给此页面以绑定树视图,如下所示
orderdetail orderdetail = new orderdetail();
orderdetail.OID = Convert.ToInt32(1);
orderdetail.Show();
Is there any event/method to bind treeview dynamically?
是否有任何事件/方法可以动态绑定树视图?
回答by Pavel Murygin
With WPF you should take advantage of MVVM pattern. I've seen a very good tutorial on using TreeView
with MVVM: http://www.codeproject.com/Articles/26288/Simplifying-the-WPF-TreeView-by-Using-the-ViewMode
使用 WPF,您应该利用 MVVM 模式。我看过一个关于使用TreeView
MVVM的非常好的教程:http: //www.codeproject.com/Articles/26288/Simplifying-the-WPF-TreeView-by-Using-the-ViewMode
回答by koly86
good template
<Grid>
<TreeView x:Name="treeView" HorizontalAlignment="Left" Height="227" Margin="10,32,0,0" VerticalAlignment="Top" Width="178" />
</Grid>
好模板
<Grid>
<TreeView x:Name="treeView" HorizontalAlignment="Left" Height="227" Margin="10,32,0,0" VerticalAlignment="Top" Width="178" />
</Grid>
and C #
和 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);
Class.GetDS(); is a static method I took from another class, you can add any additional DataSet.
Class.GetDS(); 是我从另一个类中获取的静态方法,您可以添加任何其他数据集。