【问题标题】:Binding a StaticResource to a FrameworkElement's property将 StaticResource 绑定到 FrameworkElement 的属性
【发布时间】:2011-12-11 19:00:35
【问题描述】:

我正在尝试使用作为 StaticResource 访问的 Petzold 的 LineChartGenerator(来自 http://msdn.microsoft.com/en-us/magazine/ee291567.aspx):

 <Window.Resources>
    <src:CensusData x:Key="censusData" />
    <charts:LineChartGenerator 
            x:Key="generator"
            ItemsSource="{Binding Source={StaticResource censusData}}"
            Width="300"
            Height="200">
        </charts:LineChartGenerator.VerticalAxis>
    </charts:LineChartGenerator>
</Window.Resources>

但我想将宽度和高度绑定到控件的当前宽度和高度。这不应该是:

 <charts:LineChartGenerator 
            x:Key="generator"
            ItemsSource="{Binding Source={StaticResource censusData}}"
            Width="{Binding ElementName=MyControl, Path=Width}"
            Height="200">

但这给了我一个绑定错误:找不到目标元素的管理 FrameworkElement 或 FrameworkContentElement。绑定表达式:路径=宽度;数据项=空;目标元素是'LineChartGenerator' (HashCode=52313994);目标属性是“宽度”(类型“双”)

可以这样做吗?我有各种 kludges 但他们都将控件的宽度设置为 LineChartGenerator 的宽度,这确实不是预期的效果!

谢谢

安德鲁

【问题讨论】:

    标签: wpf data-binding


    【解决方案1】:

    我相信,由于您的宽度属性位于 Window.Resources 标记中,因此宽度绑定应该看起来像这样......

    Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
        AncestorType={x:Type Window}}, Path=ActualWidth}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-16
      • 2020-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-11
      • 2014-07-02
      • 1970-01-01
      相关资源
      最近更新 更多