2010-11-11 223 views
0

我有一個xml(如果需要可以更改結構)需要綁定到wpf中的treeview控件,這樣當我點擊樹中的特定節點時,它會填充一個列表框中的更多信息也來自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,但到目前爲止沒有運氣。有沒有人有任何想法?

感謝 尼基爾

回答

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> 
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 [email protected]}" /> 
       </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.

+0

請問,這很好。我還以不同的方式工作,並將在下面發佈答案。唯一的區別是我使用的是數據觸發器,因此如果它發生變化,HDT不會直接綁定到xml。感謝您選擇電視中所選項目的鏈接。我會給你信用的答案。 – Nikhil 2010-11-11 18:40:28