2012-12-15 6 views
2

나는 WPF에서 TreeView에 레이어의 ObservableCollection을 결합했습니다 바인딩.모양 유형은

계층의 정의는 다음과 같습니다

<TreeView Grid.Column="0" 
      ItemsSource="{Binding Layers}"> 
    <TreeView.ItemTemplate> 
     <HierarchicalDataTemplate ItemsSource="{Binding SubLayers}"> 
      <StackPanel Orientation="Horizontal"> 

       <Canvas Background="LightGray"> 
        <Ellipse Fill="{Binding Color}" 
          Height="15" 
          Width="15" 
          StrokeThickness="5" 
          Stroke="{Binding Color}"/> 
       </Canvas> 

       <TextBlock Margin="20,0,0,0" Text="{Binding Path=Name}"/> 
      </StackPanel> 
     </HierarchicalDataTemplate> 
    </TreeView.ItemTemplate> 
</TreeView> 

내가 GeoType 속성을 기준으로 모양의 유형을 지정하려면 :

public class Layer 
{ 
    public Guid Id { get; set; } 
    public string Name { get; set; } 
    public string Color { get; set; } 
    public GeoType Type { get; set; } 
} 

public enum GeoType { Area, Line, Point } 

TreeView XAML입니다. 내 말은 GeoType이 XAML 위의 캔버스 대신 선인 경우 Line이어야합니다. 바인딩을 사용하여 어떻게 할 수 있습니까? 나는 변환기를 만들어야 하나?

답변

1

당신은 순수 XAML로를 사용 할 수 있습니다.

... 
<Window.Resources> 
    <DataTemplate x:Key="LineTemplate"> 
     <Line /> 
    </DataTemplate> 
    <DataTemplate x:Key="EllipseTemplate"> 
     <Ellipse /> 
    </DataTemplate> 
    ... etc 
</Window.Resources> 
... 

<TreeView Grid.Column="0" 
      ItemsSource="{Binding Layers}"> 
    <TreeView.ItemTemplate> 
     <HierarchicalDataTemplate ItemsSource="{Binding SubLayers}"> 
      <StackPanel Orientation="Horizontal"> 
       <Canvas Background="LightGray"> 
        <ContentControl Content="{Binding}"> 
         <ContentControl.Style> 
          <Style TargetType="ContentControl"> 
           <Style.Triggers> 
            <DataTrigger Binding="{Binding Path=Type}" Value="{StaticResource local:GeoType.Line}"> 
             <Setter Property="ContentTemplate" Value="{StaticResource LineTemplate}" /> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="{StaticResource local:GeoType.Ellipse}"> 
             <Setter Property="ContentTemplate" Value="{StaticResource EllipseTemplate}" /> 
            </DataTrigger> 
           </Style.Triggers> 
          </Style> 
         </ContentControl.Style> 
        </ContentControl> 
       </Canvas> 

       <TextBlock Margin="20,0,0,0" Text="{Binding Path=Name}"/> 
      </StackPanel> 
     </HierarchicalDataTemplate> 
    </TreeView.ItemTemplate> 
</TreeView> 

많은 가능한 solutions의 단지 하나이다. 지역GEOTYPE 인 네임 스페이스입니다. 데이터 바인딩을 사용하려면 리소스 내에 템플릿을 꾸며야합니다.

+0

감사합니다. 매우 도움이됩니다. Color 속성에서 도형의 색상을 어떻게 바인딩 할 수 있습니까? – Shahin

+0

감사합니다. GEOTYPE X : <계약 : 당신이 이 권리인가 "당신은 데이터 바인딩을 사용하는 리소스를 내부의 템플릿을 장식한다"고 말했다 그것은 매우 도움이 : 키 = "지역 : GeoType.Area"/> <계약 : GEOTYPE X : 키 = "지역 : GeoType.Point"/> -------- Shahin

+1

이전과 동일한 방법으로 리소스 DataTemplates. 귀하의 DataContext 레이어 유형이며, 당신은 [이 답변] (http://stackoverflow.com/questions/672991/how-do-i-convert-a-string-like-red-to-a-system) 사용할 수 있습니다 -windows-media-color)를 사용하여 문자열 대신 실제 색상을 가져옵니다. – stukselbax