2

ObservableCollectionLayer をTreeViewWPFの a にバインドしました。

レイヤーの定義は次のとおりです。

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 }

これはTreeViewXAML です:

<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プロパティに基づいて形状タイプを指定したい。上記の XAML で canvas ではなく Line である場合GeoTypeは、Line である必要があります。バインディングを使用してそれを行うにはどうすればよいですか? コンバーターを作成する必要がありますか?

4

1 に答える 1

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>

これは、考えられる多くの解決策の 1 つにすぎません。localは、GeoTypeが存在する名前空間です。データ バインディングを使用するには、リソース内のテンプレートを装飾する必要があります。

于 2012-12-15T07:56:02.157 に答える