代码之家  ›  专栏  ›  技术社区  ›  serhio

WPF绑定帮助

  •  0
  • serhio  · 技术社区  · 14 年前

    我需要将自定义依赖项属性绑定到控件内的图像元素。

    现在 Label Foreground 很好地与 TextForeground ,但不是 GeometryDrawing 在里面 Image (图像保持透明)。

    怎么了?

    <UserControl x:Class="MyStopControl"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 mc:Ignorable="d" Height="12" Width="24">
        <Canvas >
            <Image x:Name="Dot" Canvas.Left="0" Canvas.Top="0">
                <Image.Source>
                    <DrawingImage>
                        <DrawingImage.Drawing>
                            <DrawingGroup>
                                <GeometryDrawing>
                                    <GeometryDrawing.Pen>
                                        <Pen Brush="{Binding RelativeSource={x:Static RelativeSource.Self},Path=TextForeground}" 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.Pen>
                                        <Pen Brush="{Binding RelativeSource={x:Static RelativeSource.Self},Path=TextForeground}"  Thickness="1"/>
                                    </GeometryDrawing.Pen>
                                    <GeometryDrawing.Geometry>
                                        <GeometryGroup>
                                            <EllipseGeometry x:Name="MediumCircle" Center="0,0" RadiusX="4" RadiusY="4"/>
                                        </GeometryGroup>
                                    </GeometryDrawing.Geometry>
                                </GeometryDrawing>
                                <GeometryDrawing Brush="{Binding RelativeSource={x:Static RelativeSource.Self},Path=TextForeground}">
                                    <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>
    
            <Border x:Name="StopShadow"
                    Background="{Binding ElementName=TextBackground}" 
                    LayoutTransform="{Binding ElementName=StopText, Path=LayoutTransform}">
                <Label x:Name="StopLabel" 
                       Content="Bla bla some text" 
                       Foreground="{Binding ElementName=TextForeground}" />
            </Border>
    
        </Canvas>
    </UserControl>
    
    2 回复  |  直到 14 年前
        1
  •  3
  •   Aaron McIver    14 年前

    GeometryDrawing 没有 TextForeground 财产。你指的是自我 几何制图 . 改变你的 RelativeSource 如果你想抓住 文本前景 从一个不同的控制。

    <GeometryDrawing.Pen>
        <Pen Brush="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=TextForeground}" Thickness="1"/>
    </GeometryDrawing.Pen>
    
        2
  •  3
  •   serhio    14 年前
    <UserControl x:Name="MyStopControl" >
        ...
        <Pen Brush="{Binding ElementName=MyStopControl, Path=TextForeground}"/>
        ...
    </UserControl>