【问题标题】:Passing a style down to the child of a UserControl将样式向下传递给 UserControl 的子级
【发布时间】:2012-04-24 05:41:18
【问题描述】:

所以我正在编写一个由其他几个控件组合而成的 UserControl。特别是一个图表、另一个自定义日期范围控件和用于填充屏幕其他区域的 ContentPresenters。

所以我使用{Binding ElementName=chartView, Path=RightContent} 很好地将我的内容演示者绑定到依赖属性,我想如果我想将可选的数据点样式传递给实际的图表控件,我可以执行类似{Binding ElementName=chartView, Path=DataPointStyle} 的操作来绑定到Style 类型的依赖属性。

不幸的是,这给了我错误System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=chartView'. BindingExpression:Path=DataPointStyle; DataItem=null; target element is 'ColumnSeries' (Name=''); target property is 'DataPointStyle' (type 'Style')

所以我认为 Chart 和 ColumnSeries 控件的工作方式存在一些奇怪的 XAML 范围界定问题。它是 2010 年 2 月 WPF 工具包中的图表控件,所以如果有更新的东西我应该使用,那也很酷。

所以基本上:

<UserControl x:Class="MyFramework.Controls.ChartView"
         x:Name="chartView"
         ...>

    ...

    <!-- Works great -->
    <ContentPresenter Content="{Binding ElementName=chartView, Path=TopContent}" />

    <!-- Doesn't work -->
    <charting:Chart>
        <charting:ColumnSeries DataPointStyle="{Binding ElementName=chartView, Path=DataPointStyle}" />
        </charting:Chart>
    </charting:Chart>


    ...
</UserControl>

如何使 ColumnSeries.DataPointStyle 上的第二个绑定起作用?

【问题讨论】:

    标签: wpf xaml data-binding user-controls


    【解决方案1】:

    我从未遇到过这样的问题,但作为一种解决方法,您可以尝试使用RelativeSourceFindAncestor 模式的绑定。

    【讨论】:

    • 我已经尝试过{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type controls:ChartView}}, Path=DataPointStyle},但我仍然以System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='MyFramework.Controls.ChartView', AncestorLevel='1''. BindingExpression:Path=DataPointStyle; DataItem=null; target element is 'ColumnSeries' (Name=''); target property is 'DataPointStyle' (type 'Style')结束
    猜你喜欢
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多