2011-12-11 130 views
0

我试图用Petzold的LineChartGenerator(从http://msdn.microsoft.com/en-us/magazine/ee291567.aspx):绑定一个静态资源,其作为一个静态资源访问的FrameworkElement的财产

<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上。 BindingExpression:路径=宽度;的DataItem = NULL;目标元素是'LineChartGenerator'(HashCode = 52313994);目标属性是'宽度'(类型'Double')

是否可以这样做?我有各种各样的kludges,但他们都将teh控件的宽度设置为LineChartGenerator的宽度,这实际上不是所需的效果!

感谢

安德鲁

回答

1

我相信,因为你width属性是在Window.Resources标签宽度结合应该是这个样子......

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

不错的尝试,保罗,但这给出了相同的绑定错误 – amaca