0

基本的に一種のTreeViewであるカスタムコントロールがあります。ここで、TreeView コントロールに任意のレベルの詳細を含める必要があるため、次のデータ テンプレートを思いつきました。

次の Generic.xaml があります

<DataTemplate x:Key="treetemplate">
    <StackPanel>
     <TextBlock Text="{Binding Label}" ></TextBlock>
     <ItemsControl ItemsSource="{Binding Children}" ItemTemplate="{DynamicResource treetemplate}" />
    </StackPanel>
</DataTemplate>

<ControlTemplate TargetType="{x:Type local:CustomControl1}" x:Key="testkey">
    <ControlTemplate.Resources>
        <local:AnythingToListConverter x:Key="anyconv"></local:AnythingToListConverter>
    </ControlTemplate.Resources>
    <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
        <ItemsControl ItemsSource="{Binding Root, Converter={StaticResource anyconv}}" ItemTemplate="{StaticResource treetemplate}"  />
    </Border>
</ControlTemplate>

<Style TargetType="{x:Type local:CustomControl1}">
    <Setter Property="Template" Value="{StaticResource testkey}" />
</Style>

これが私のカスタムコントロールコンストラクターです

        this.Root = new Node();

        this.Root.Label = "Root";
        this.Root.Children = new List<Node>();

        this.Root.Children.Add(new Node(){Label="Child1"});
        this.DataContext = this;

そして、これはコントロールがどのように見えるかです

コントロール

これが私が問題だと思うのは 、同じテンプレートの再帰呼び出しについて、私が使用してDynamicResourceいる. これは私のものでは機能せず、実際のリソースは呼び出されませんでした。それを に変更するとStaticResource、それ自体が認識されないため、コンパイルされません。どうすれば修正できますか?

完全なソリューションは、ここからダウンロードできます

4

1 に答える 1