3

更新:这是因为我安装了 VS2012 RC。(呃,对不起应该说)。我有一个 WPF 切换按钮。我想在上面画一张图,所以我做了一个画笔,我想控制画的颜色,所以我把它绑定到了ToggleButton的Foreground属性。不过好像没用?(在下面的示例中,绘图本来是蓝色的,但它是黑色的)。

<UserControl x:Class="SynthEditWpf.UserControl1"
             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" 
             d:DesignHeight="300" d:DesignWidth="300">
        <UserControl.Resources>

        <DrawingBrush x:Key="Power" Stretch="None">
            <DrawingBrush.Drawing>
                <DrawingGroup>
                    <DrawingGroup.Children>
                        <GeometryDrawing Geometry="F1 M 11.9999,4.34296L 11.9999,9.57471">
                            <GeometryDrawing.Pen>
                                <Pen Thickness="3" StartLineCap="Round" EndLineCap="Round" LineJoin="Round" Brush="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type Control}}}"/>
                            </GeometryDrawing.Pen>
                        </GeometryDrawing>
                        <GeometryDrawing Geometry="F1 M 7.60373,6.78986C 6.09436,8.03101 5.13317,9.90375 5.13317,11.999C 5.13317,15.7359 8.19123,18.7654 11.9635,18.7654C 15.7359,18.7654 18.7939,15.7359 18.7939,11.999C 18.7939,9.90375 17.8327,8.03101 16.3234,6.78986">
                            <GeometryDrawing.Pen>
                                <Pen Thickness="3" StartLineCap="Round" EndLineCap="Round" LineJoin="Round" Brush="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type Control}}}"/>
                            </GeometryDrawing.Pen>
                        </GeometryDrawing>
                    </DrawingGroup.Children>
                </DrawingGroup>
            </DrawingBrush.Drawing>
        </DrawingBrush>
    </UserControl.Resources>
    <Grid>
        <ToggleButton Foreground="Blue" Width="30" Height="30">
            <Rectangle Fill="{StaticResource Power}" Width="24" Height="24" />
        </ToggleButton>
    </Grid>
</UserControl>

跟踪输出

System.Windows.Data 警告:55:为绑定创建 BindingExpression (hash=9381496) (hash=30868550) System.Windows.Data 警告:57:路径:'Foreground' System.Windows.Data 警告:59:BindingExpression (hash= 9381496):默认模式解析为 OneWay System.Windows.Data 警告:60:BindingExpression(哈希=9381496):默认更新触发器解析为 PropertyChanged System.Windows.Data 警告:61:BindingExpression(哈希=9381496):附加到系统。 Windows.Media.Pen.Brush (hash=13172414) System.Windows.Data 警告:65:BindingExpression (hash=9381496):RelativeSource (FindAncestor) 需要树上下文 System.Windows.Data 警告:64:BindingExpression (hash=9381496) : 解决源延迟

4

1 回答 1

7

这对我有用。好的,我最好解释一下,嘿。当它只是作为资源的画笔时,它不是矩形的一部分。它找到的祖先实际上是 userControl。尝试改变用户控件的前景,你会看到它改变了你的笔的颜色。要将其作为矩形的一部分,因此您需要将其包装成一种样式(见下文)

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">   
    <Window.Resources>

    <Style x:Key="Frank" TargetType="Rectangle">
        <Style.Resources>
            <DrawingBrush x:Key="Power" Stretch="None">
                <DrawingBrush.Drawing>
                    <DrawingGroup>
                        <DrawingGroup.Children>
                            <GeometryDrawing Geometry="F1 M 11.9999,4.34296L 11.9999,9.57471">
                                <GeometryDrawing.Pen>
                                    <Pen Thickness="3" StartLineCap="Round" EndLineCap="Round" LineJoin="Round" Brush="{Binding RelativeSource={RelativeSource AncestorType=ToggleButton, Mode=FindAncestor}, Path=Foreground}"/>
                                </GeometryDrawing.Pen>
                            </GeometryDrawing>
                            <GeometryDrawing Geometry="F1 M 7.60373,6.78986C 6.09436,8.03101 5.13317,9.90375 5.13317,11.999C 5.13317,15.7359 8.19123,18.7654 11.9635,18.7654C 15.7359,18.7654 18.7939,15.7359 18.7939,11.999C 18.7939,9.90375 17.8327,8.03101 16.3234,6.78986">
                                <GeometryDrawing.Pen>
                                    <Pen Thickness="3" StartLineCap="Round" EndLineCap="Round" LineJoin="Round" Brush="{Binding RelativeSource={RelativeSource AncestorType=ToggleButton, Mode=FindAncestor}, Path=Foreground}"/>
                                </GeometryDrawing.Pen>
                            </GeometryDrawing>
                        </DrawingGroup.Children>
                    </DrawingGroup>
                </DrawingBrush.Drawing>
            </DrawingBrush>
        </Style.Resources>
        <Setter Property="Fill" Value="{StaticResource Power}"/>
    </Style>
</Window.Resources>
    <Grid>
        <ToggleButton Foreground="Blue" Width="30" Height="30">
            <Rectangle Style="{StaticResource Frank}" Width="24" Height="24"  />
        </ToggleButton>
    </Grid>

于 2012-07-26T04:41:37.943 回答