c# - 形状类型绑定(bind)

标签 c# wpf

我已将 Layer 的 ObservableCollection 绑定(bind)到 WPF 中的 TreeView

图层定义是:

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:

<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 属性指定形状类型。我的意思是,如果 GeoType 在上面的 XAML 中是 Line 而不是 canvas,那么它应该是 Line。 我如何使用绑定(bind)来做到这一点?我应该创建转换器吗?

最佳答案

您可以使用 和纯 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>

这只是众多可能解决方案中的一种。 local 是您的GeoType 所在的命名空间。您应该装饰资源内的模板以使用数据绑定(bind)。

关于c# - 形状类型绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13890130/

相关文章:

c# - 当我需要多个相同类型的关系时 Entity Framework 出现问题

c# - Chrome 在 div 之外溢出 iframe 背景?

c# - PerformanceCounter 连接远程服务器时速度极慢

c# - 如何组合多个 GeometryGroup 对象?

C# UWP ScrollViewer 改变 Horizo​​ntalScrollBarVisibility

wpf - 如何在 WPF RichTextBox 中将插入符号移动一定数量的位置?

.net - 在WPF世界中有6个月没有使用过的混合物,我应该使用它吗?

c# - WPF 组合框值和显示文本

wpf - 将 SelectedValue 绑定(bind)到 WPF 组合框

c# - 使用 UIElement 将 FlowDocument 导出为 rtf