【问题标题】:Fix binding error for a binding applied in a style修复样式中应用的绑定的绑定错误
【发布时间】:2015-08-18 19:28:33
【问题描述】:

当我运行我的应用程序时,我的输出面板中出现绑定错误:

System.Windows.Data 错误:4:找不到与引用“RelativeSource FindAncestor,AncestorType='System.Windows.Shapes.Ellipse',AncestorLevel='1''的绑定源。绑定表达式:路径=宽度;数据项=空;目标元素是“TranslateTransform”(HashCode=59715965);目标属性是“X”(类型“双”)

我正在尝试创建一种样式,该样式将在 Canvas 中的所有椭圆上应用 TranslateTransform。绑定有效,但是当我启动我的应用程序时,我的输出面板中出现错误。我该如何解决这个错误?

编辑:绑定确实有效,我只是想摆脱 Visual Studio 输出窗口中的错误。

如果我将 RenderTransform 从样式移到 Ellipse 标记中,错误就会消失,但我希望在样式中使用此变换,因为它需要应用于许多椭圆。

这是我正在使用的 XAML:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="WpfApplication1.MainWindow"
        Title="MainWindow" Height="300" Width="300">
    <Window.Resources>
        <Style TargetType="Ellipse">
            <Setter Property="RenderTransform">
                <Setter.Value>
                    <TranslateTransform>
                        <TranslateTransform.X>
                            <Binding 
                                    RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type Ellipse}}"
                                    Path="Width"/>
                        </TranslateTransform.X>
                    </TranslateTransform>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Canvas>
        <Ellipse Width="100" Height="100" Fill="Red" />
    </Canvas>
</Window>

【问题讨论】:

  • 这段代码对我有用。在这两种情况下:Window.Resources 和 Ellipse.Resources VS2013
  • 我也在使用 VS2013,代码确实有效,但在输出窗口中出现了奇怪的错误。运行代码时你的输出窗口是空的吗?
  • Window 内只有红色的变形椭圆。
  • 我不太明白这段代码想要达到什么目的。您有一种将TranslateTransform.X 绑定到其父Ellipse 的样式?为什么需要绑定?为什么不直接使用{TemplateBinding Width}
  • @MikeEason 是的,我想将TranslateTransform.X 绑定到它的父Ellipse。我不能使用TemplateBinding,因为我没有使用模板,我使用的是样式。我可以用别的吗?

标签: wpf xaml


【解决方案1】:
猜你喜欢
  • 2015-05-21
  • 2021-07-10
  • 1970-01-01
  • 2016-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多