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();
是否有任何事件/方法可以动态绑定树视图?