绑定WPF图像的属性

时间:2010-11-08 18:54:18

标签: wpf data-binding user-controls

我有以下WPF UserControl
alt text

我希望只有当对象的(依赖bool属性)属性 IsSelected 为true时才会显示蓝色边框。

这可能吗?

<Canvas>

    <Image x:Name="Dot">
        <Image.Source>
            <DrawingImage>
                <DrawingImage.Drawing>
                    <DrawingGroup>
                        <GeometryDrawing>
                            <GeometryDrawing.Pen>
                                <Pen Brush="Blue" Thickness="2" x:Name="BigCircleThickness"/>
                            </GeometryDrawing.Pen>
                            <GeometryDrawing.Geometry>
                                <GeometryGroup>
                                    <EllipseGeometry x:Name="BigCircle" Center="0,0" RadiusX="7" RadiusY="7"/>
                                </GeometryGroup>
                            </GeometryDrawing.Geometry>
                        </GeometryDrawing>
                        <GeometryDrawing>
                            <GeometryDrawing.Brush>
                                <SolidColorBrush Color="Blue" />
                            </GeometryDrawing.Brush>
                            <GeometryDrawing.Geometry>
                                <GeometryGroup>
                                    <EllipseGeometry x:Name="SmallCircle" Center="0,0" RadiusX="2" RadiusY="2"/>
                                </GeometryGroup>
                            </GeometryDrawing.Geometry>
                        </GeometryDrawing>
                    </DrawingGroup>
                </DrawingImage.Drawing>
            </DrawingImage>
        </Image.Source>
    </Image>

</Canvas>

换句话说,当IsSeleted = false“BigCircle”消失时,我需要这样做。

这可能吗?

1 个答案:

答案 0 :(得分:0)

您可以使用DataTrigger。像这样:

<Image>
    <Image.Style>
        <Setter Property="Source" Value={StaticResource MyDrawingImage_NoBorder}" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsSelected}" Value="True">
                <Setter Property="Source" Value={StaticResource MyDrawingImage_WithBorder}" />
            </DataTrigger>
        </Style.Triggers>
    </Image.Style>
</Image>