Domanda

Con il seguente XML sottostante vorrei utilizzare un hierarchicaldatatemplate con un WPF TreeView.

A seconda del valore in Tipo Intendo utilizzare immagini diverse con il nome accanto ad esso e quindi ottenere il nome dal valore dal tag ID. .

    <key type='Configuration'>
        <id field='name' value='Some value'/>
    </key>
    <key type='Container'>
        <id field='name' value='MyName'/>
        <key type='Container'>
            <id field='name' value='Data12345'/>
            <key type='Container'>
                <id field='name' value='Data987655'/>
                <key type='Circuit'>
                    <id field='name' value='Data63236723'/>
                </key>
            </key>
        </key>
    </key>
.

Ho provato alcuni dei semplici esempi, ma nessuno di loro mostra come usare HierachicalDatatemplate con attributi e anche come ottenere il testo con legame dagli attributi.

Sarebbe bello se qualcuno potesse mostrare come sembrare il gerarchicoDatatemplate per questo XML da utilizzare con una visione Tree.

È stato utile?

Soluzione

All'inizio, ho pensato di non poter soddisfare tali requisiti con quello schema XML.Tuttavia, dopo averlo provato in un progetto di test, sembra che tutto funzioni bene:

Inserisci Descrizione dell'immagine qui

Dovrai utilizzare un XmlDataProvider per accedere al file XML:

<XmlDataProvider Source="/WpfApplication2;component/Xml/TestXMLFile.xml" 
    XPath="root/key" />
.

Dovrai anche aggiungere un nodo root all'XML per renderlo legale:

<?xml version="1.0" encoding="utf-8" ?>
<root>
    <key type='Configuration'>
        <id field='name' value='Some value'/>
    </key>
    <key type='Container'>
        <id field='name' value='MyName'/>
        <key type='Container'>
            <id field='name' value='Data12345'/>
            <key type='Container'>
                <id field='name' value='Data987655'/>
                <key type='Circuit'>
                    <id field='name' value='Data63236723'/>
                </key>
            </key>
        </key>
    </key>
</root>
.

Quindi è necessario aggiungere il TreeView:

<TreeView ItemsSource="{Binding}" ItemTemplate="{StaticResource ItemTemplate}" />
.

E poi, infine, aggiungi il HierarchicalDataTemplate nella sezione Resources:

<HierarchicalDataTemplate x:Key="ItemTemplate" ItemsSource="{Binding XPath=key}"  DataType="key">
    <StackPanel Orientation="Horizontal" Margin="0,2">
        <Image>
            <Image.Style>
                <Style>
                    <Setter Property="Image.Source" Value="Images/Default.png" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding XPath=@type}" Value="Container">
                            <Setter Property="Image.Source" Value="Images/Container.png" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding XPath=@type}" Value="Configuration">
                            <Setter Property="Image.Source" Value="Images/Configuration.png" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding XPath=@type}" Value="Circuit">
                            <Setter Property="Image.Source" Value="Images/Circuit.png" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Image.Style>
        </Image>
        <TextBlock Text="{Binding XPath=id/@value}" Margin="5,0" />
    </StackPanel>
</HierarchicalDataTemplate>
.

Non intendevo davvero fare il tutto per te, quindi ti lascerò per modificarlo a tuo piacimento.Confido che sarai bene da qui.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top