L'utilisation de XAML resourceDictionary dans le code

c'est resourcedictionay fichier: TopologyTree.xaml

<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:autoDraw.ViewModel.Topology"
>

<HierarchicalDataTemplate x:Key="TopologyTreeBase" DataType="{x:Type local:Base}" ItemsSource="{Binding children}">
    <StackPanel Orientation="Horizontal">
        <CheckBox IsChecked="{Binding IsChecked}"></CheckBox>
        <TextBlock Text="{Binding name}"></TextBlock>
    </StackPanel>
</HierarchicalDataTemplate>

</ResourceDictionary>

Côté C#

objectTree.Resources = new ResourceDictionary();
objectTree.Resources.Source = new Uri("GUI/TopologyTree.xaml", UriKind.Relative);

tout objectTree est un TreeView

comment jamais cela ne fonctionne pas.

J'ai essayé de suivre ce qui a fonctionné, mais j'ai besoin de redéfinir le Type de données ici, donc je pense que ce n'est pas si bon.

var resourceDictionary = new ResourceDictionary();
resourceDictionary.Source = new Uri("GUI/TopologyTree.xaml", UriKind.Relative);

objectTree.Resources.Add(
    new DataTemplateKey(typeof(ViewModel.Topology.Base)),
    resourceDictionary["TopologyTreeBase"] as HierarchicalDataTemplate
);

De plus, j'ai essayé de mettre le contenu de xaml dans le xmal fenêtre directement suivie, Qui fonctionne, mais j'en ai besoin d'être chargé dyanmically, donc il a juste prouvé que mon xmal est bon.

    <TreeView Name="objectTree" Grid.Column="4" Margin="3" Grid.Row="1" Grid.RowSpan="3">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type local:Topology.Base}" ItemsSource="{Binding children}">
                <StackPanel Orientation="Horizontal">
                    <CheckBox IsChecked="{Binding IsChecked}"></CheckBox>
                    <TextBlock Text="{Binding name}"></TextBlock>
                </StackPanel>
            </HierarchicalDataTemplate>
        </TreeView.Resources>
    </TreeView>

Quelqu'un pourrait-il m'aider à avoir un moyen de l'utiliser en C#, côté simplement?

Vous dire le nom du fichier est TopologyTreeDisplay.xaml pourtant, vous essayez de charger TopologyTree.xaml sans affichage partie
sorrsy, c'est TopologyTree.xaml extact, j'ai fait une erreur dans le post

OriginalL'auteur Enzojz | 2013-02-14