【问题标题】:Help with WPF Binding帮助 WPF 绑定
【发布时间】:2010-11-18 16:21:04
【问题描述】:

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

现在Label 的Foreground 很好地绑定到TextForeground,但不是Image 内的GeometryDrawing(图像保持透明)。

怎么了?

<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>

【问题讨论】:

    标签: wpf data-binding binding


    【解决方案1】:

    GeometryDrawing 没有TextForeground 属性。您正在引用 Self,即 GeometryDrawing。如果您尝试从其他控件中获取 TextForeground,请更改您的 RelativeSource。

    <GeometryDrawing.Pen>
        <Pen Brush="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=TextForeground}" Thickness="1"/>
    </GeometryDrawing.Pen>
    

    【讨论】:

    • MyStopControl 具有 TextForeground 属性。所以通过例如对于标签 > Foreground="{Binding ElementName=TextForeground}" 效果很好。
    • @serhio 您在 GeometryDrawing 上引用了 RelativeSource={x:Static RelativeSource.Self},但在标签中您不是
    • 那么我应该如何更改 Source 以指向用户控件本身?
    • 还发现了另一个变体(顺便说一句,我用过)谢谢!
    【解决方案2】:
    <UserControl x:Name="MyStopControl" >
        ...
        <Pen Brush="{Binding ElementName=MyStopControl, Path=TextForeground}"/>
        ...
    </UserControl>
    

    【讨论】:

      猜你喜欢
      • 2010-11-17
      • 2011-10-13
      • 1970-01-01
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      • 2011-03-16
      • 2012-11-18
      • 1970-01-01
      相关资源
      最近更新 更多