我有一个xml(如果需要可以更改结构)需要绑定到wpf中的treeview控件,这样当我点击树中的特定节点时,它会填充一个包含更多信息的列表框它也来自xml。
我的xml如下:
<Event locale="en-US">
<Eventid Name="Mer101">
<EventType>StoredProc</EventType>
<EventParameters>
<EventParameter>Field1</EventParameter>
<EventParameter>Field2</EventParameter>
</EventParameters>
<ErrorMessage>Error_Msg_Localized_ID {Field1} {Field2}...</ErrorMessage>
<EventStartDate>DateTime</EventStartDate>
<EventEndDate>DateTime</EventEndDate>
</Eventid>
<Eventid Name="Ora101">
<EventType>LowLevelEvents</EventType>
<EventParameters>
<EventParameter>Field1</EventParameter>
</EventParameters>
<ErrorMessage>Error message {Field1}...</ErrorMessage>
<EventStartDate>DateTime</EventStartDate>
<EventEndDate>DateTime</EventEndDate>
</Eventid>
<Eventid Name="Ora102">
<EventType>LowLevelEvents</EventType>
<EventParameters>
<EventParameter>Field1</EventParameter>
<EventParameter>Field2</EventParameter>
</EventParameters>
<ErrorMessage>Error message {Field1} {Field2}...</ErrorMessage>
<EventStartDate>DateTime</EventStartDate>
<EventEndDate>DateTime</EventEndDate>
</Eventid>
因此树视图应如下所示:
+Rootnode
-Mer101
-Ora101
-Ora102
例如,当用户点击Mer101时,列表框界限显示xml中的ErrorMessage。
我曾尝试使用hierarchicaldatatemplate并尝试使用xdocument xmldataprovider但到目前为止没有运气。有没有人有任何想法?
由于 NIKHIL
答案 0 :(得分:1)
至于树视图,这是有效的(在Kaxaml中拍打它):
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<XmlDataProvider
x:Key="events"
XPath="/Events">
<x:XData>
<Events
xmlns="">
<Eventid
Name="Mer101">
<EventType>StoredProc</EventType>
<EventParameters>
<EventParameter>Field1</EventParameter>
<EventParameter>Field2</EventParameter>
</EventParameters>
<ErrorMessage>Error_Msg_Localized_ID {Field1} {Field2}...</ErrorMessage>
<EventStartDate>DateTime</EventStartDate>
<EventEndDate>DateTime</EventEndDate>
</Eventid>
<Eventid
Name="Ora101">
<EventType>LowLevelEvents</EventType>
<EventParameters>
<EventParameter>Field1</EventParameter>
</EventParameters>
<ErrorMessage>Error message {Field1}...</ErrorMessage>
<EventStartDate>DateTime</EventStartDate>
<EventEndDate>DateTime</EventEndDate>
</Eventid>
<Eventid
Name="Ora102">
<EventType>LowLevelEvents</EventType>
<EventParameters>
<EventParameter>Field1</EventParameter>
<EventParameter>Field2</EventParameter>
</EventParameters>
<ErrorMessage>Error message {Field1} {Field2}...</ErrorMessage>
<EventStartDate>DateTime</EventStartDate>
<EventEndDate>DateTime</EventEndDate>
</Eventid>
</Events>
</x:XData>
</XmlDataProvider>
</Page.Resources>
<Grid>
<TreeView
x:Name="tv"
ItemsSource="{Binding Source={StaticResource events}}">
<TreeView.Resources>
<HierarchicalDataTemplate
DataType="Eventid">
<TextBlock
Text="{Binding XPath=@Name}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate
DataType="Events"
ItemsSource="{Binding XPath=*}">
<TextBlock
Text="Expand to view Events" />
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
</Grid>
</Page>
不幸的是,您无法绑定到TreeView的SelectedItem。为了知道选择了哪个项目,check out this question on the subject.
答案 1 :(得分:0)
我找到了一种方法,无需将HDT绑定到xml。这是xaml:
<Window x:Class="DBUpgradeUI.DBUpgEventReportingResolution"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DBUpgEventReportingResolution" Height="453" Width="549" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded">
<Window.Resources>
<HierarchicalDataTemplate x:Key="TVTemplate">
<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" />
</Window.Resources>
<Grid>
<TreeView Name="treeview" ItemsSource="{Binding Source={StaticResource xmlDataProvider},XPath=*}"
Margin="0,37,0,0" HorizontalAlignment="Left" Width="198"
ItemTemplate="{StaticResource TVTemplate}"
/>
</Grid>
和xmldataprovider背后的代码如下:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(advEventsXML);
XmlDataProvider dataProvider = this.FindResource("xmlDataProvider") as XmlDataProvider;
dataProvider.Document = xmlDoc;
treeview.DataContext = dataProvider;
我正在使用的示例xml示例如下:
<AllEvents>
<Meridium>Mer101
<Type>StoredProcedure</Type>
<EventParameters>Field1</EventParameters>
<ErrorMessage>Error_Msg_Localized_ID {Field1} {Field2}...</ErrorMessage>
<EventStartDate>DateTime</EventStartDate>
<EventEndDate>DateTime</EventEndDate>
</Meridium>
<OtherEvents>
<Database>Ora101
<Type>LowerLevelEvents</Type>
<EventParameters>
<Parameter>Field1</Parameter>
<Parameter>Field2</Parameter>
</EventParameters>
<ErrorMessage>Error_Msg_Localized_ID {Field1} {Field2}...</ErrorMessage>
<EventStartDate>DateTime</EventStartDate>
<EventEndDate>DateTime</EventEndDate>
</Database>
<Database>Ora102
<Type>LowerLevelEvents</Type>
<EventParameters>
<Parameter>Field1</Parameter>
<Parameter>Field2</Parameter>
</EventParameters>
<ErrorMessage>Error_Msg_Localized_ID {Field1} {Field2}...</ErrorMessage>
<EventStartDate>DateTime</EventStartDate>
<EventEndDate>DateTime</EventEndDate>
</Database>
</OtherEvents> </AllEvents>