기본적으로 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
으로 변경하면 컴파일되지 않습니다. 자체가 보이지 않기 때문입니다. 어떻게 수정해야합니까?
전체 솔루션은 downloaded here 일 수 있습니다.