为什么我的绑定到前景色的DrawingBrush填充的ToggleButton不起作用?VS2012

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:为绑定(hash=30868550)创建了BindingExpression(hash=9381496)。 System.Windows.Data警告:57:路径:'Foreground' System.Windows.Data警告:59:BindingExpression(hash=9381496):默认模式解析为OneWay System.Windows.Data警告:60:BindingExpression(hash=9381496):默认更新触发程序解析为PropertyChanged System.Windows.Data警告:61:BindingExpression(hash=9381496):附加到System.Windows.Media.Pen.Brush(hash=13172414) System.Windows.Data警告:65:BindingExpression(hash=9381496):RelativeSource(FindAncestor)需要树上下文 System.Windows.Data警告:64:BindingExpression(hash=9381496):解析源被延迟


展示一下你的绑定错误。另外,我希望你是在谈论运行时,如果不是的话,最好纠正一下。 - H.B.
你能否尝试在DrawingBrush资源中设置前景色? - TMan
控制台窗口中没有绑定错误。不要在设计器中工作,也不要在运行时工作。 - Jeff McClintock
抱歉,不理解“在Drawingbrush资源中设置前景?” DrawingBrush和DrawingGroup没有该属性。我可以直接将Pen的Brush设置为“Red”-没有问题。但绑定是个问题。 - Jeff McClintock
(在问题中添加了跟踪输出) - Jeff McClintock
1个回答

7

这适用于我。好的,让我解释一下。当它只是一个刷子资源时,它并不是矩形的一部分。它找到的祖先实际上是用户控件。尝试更改用户控件的前景色,您将看到它更改笔的颜色。为了将其作为矩形的一部分,因此也是togglebutton,您需要将其包装在样式中(见下文)。

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


那么,画笔不是矩形的“后代”,而是定义画笔的地方的“后代”?(在我的情况下是UserControl)。 因此,要有几个带有单独图片的按钮,我需要几种样式吗?由于每个按钮在我的情况下都是唯一的,我不需要共享图纸,我想我可以将图纸直接放在按钮xaml中? - Jeff McClintock
拥有多种不同的样式与拥有多个DrawingBrush资源并没有太大的区别。请记住,您可以始终继承样式,因此不应该有太多重复。但是,如果每个按钮都是唯一的,请将其放在按钮中。如果您需要该按钮的多个实例,请使用样式或创建自定义控件。 - Nogusta

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接