【问题标题】:How may my Style Setter get at the DataContext of the of the element whose style is being set?我的样式设置器如何获取正在设置样式的元素的 DataContext?
【发布时间】:2020-08-11 12:24:54
【问题描述】:

这似乎应该很简单,但我想不通。

我正在尝试更改元素上“填充”属性的工作样式设置器,以在 Setter.Value 中的 XAML 中实际声明 SolidColorBrush 对象。不幸的是,在我的画笔声明中,我似乎无法“获取”我试图设置其 Fill 的对象的 DataContext。我该怎么做?

下面是带有 setter 的样式。它使用一个转换器,该转换器接受一个枚举值(“Usage”)并返回一个颜色值。转换器很好,但绑定失败,因为它找不到对象。

<core:UsageToColorConverter x:Key="CvtUsageToColor"/>

<Style x:Key="RegionBandStyle" 
       TargetType="{x:Type tk:CartesianPlotBandAnnotation}">

    <!--  Help Intellisense show us the correct bindings when coding  -->

    <d:Style.DataContext>
        <x:Type Type="gci:IProfileRegion" />
    </d:Style.DataContext>


    <Setter Property="Fill">
        <Setter.Value>
            <SolidColorBrush >
                <SolidColorBrush.Color>
                    <!-- THIS BINDING FAILS:  It cannot find the "ancestor" CartesianPlotBandAnnotation
                         in order to get at its DataContext
                    -->
                    <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type tk:CartesianPlotBandAnnotation}}" 
                        Path="DataContext.(gci:IProfileRegion.Usage)"
                        Converter="{StaticResource CvtUsageToColor}"/>
                </SolidColorBrush.Color>
            </SolidColorBrush>
        </Setter.Value>
    </Setter>

</Style>

绑定失败并显示此消息:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='Telerik.Windows.Controls.ChartView.CartesianPlotBandAnnotation', AncestorLevel='1''. BindingExpression:Path=DataContext.Usage; DataItem=null; target element is 'SolidColorBrush' (HashCode=39731869); target property is 'Color' (type 'Color')

我想这是有道理的。我模糊的理解是画笔不在视觉(或逻辑)树中,对吧?所以它找不到祖先?是这个原因吗?

这个 Setter 的原始版本可以工作,但要简单得多。它只是使用了一个类似的转换器,它返回 SolidColorBrush 而不是颜色:

    <Setter Property="Fill"  Value="{Binding Usage, Converter={StaticResource CvtUsageToBrush}}"/>

这很好,但不幸的是(出于不相关的原因),我需要以另一种方式做事;我需要自己使用属性元素语法明确声明画笔

谁能告诉我我需要什么绑定巫术才能在我的SolidColorBrush 颜色绑定中获取CartesianPlotBandAnnotation 的DataContext?

(无论我在逻辑树和视觉树上阅读多少次,这种绑定问题都会让我头晕目眩。我的搜索不断提出相关主题,但不是我想要的。)

【问题讨论】:

  • 我可以请您访问my question吗?猜你知道答案。
  • 我三思而后行,只有RelativeSource="{RelativeSource Self}"
  • 不幸的是,这似乎不起作用。我尝试过这个。我没想到会这样,因为它只是让我回到我正在声明的同一个 SolidColorBrush。 SolidColorBrush 没有 DataContext,因为它不是 FrameworkElement

标签: wpf data-binding visual-tree


【解决方案1】:

RelativeSource 的 FindAncesstor 模式可用于在可视树中查找父项。然而,这种情况并非如此。画笔颜色应该继承元素数据上下文。尝试完全删除 RelativeSource 设置器。另外,您确定 Usage 是附加属性吗?如果没有,只需在路径中设置“使用”。 如果这没有帮助,至少您会收到一条新的错误消息,说明 CartesianPlotBandAnnotation 的 DataContext 中实际提供了哪个对象。

更新: 在接口绑定的情况下,将 Path 设置为“(gci:IProfileRegion.Usage)”就足够了。我刚刚测试了这段代码并确认它可以正常工作:https://github.com/Drreamer/ColorInterfaceBinding 如果它在您的项目中不起作用,请说明在这种情况下引发了什么异常。它有助于找到问题的确切原因。

【讨论】:

  • 您好,谢谢您的回答,我可能不太清楚。 CartesianPlotBandAnnotation 的 DataContext 是正确的。实际上,它是我的接口 (IProfileRegion) 的一个实例,因为我应该知道这一点,因为该样式中的其他绑定(我承认没有显示)确实针对该数据上下文起作用。而我较旧的“填充”绑定也适用于它。我看到的问题是我试图声明的 SolidColorBrush 似乎无法访问它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-04
相关资源
最近更新 更多