繁体   English   中英

将StaticResource绑定到FrameworkElement的属性

[英]Binding a StaticResource to a FrameworkElement's property

我正在尝试使用Petzold的LineChartGenerator(来自http://msdn.microsoft.com/zh-cn/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。 BindingExpression:Path = Width; DataItem = null; 目标元素是“ LineChartGenerator”(HashCode = 52313994); 目标属性为“宽度”(类型为“双精度”)

是否有可能做到这一点? 我有各种各样的动作,但是它们都将控件的宽度设置为LineChartGenerator的宽度,这实际上不是所希望的效果!

谢谢

安德鲁

我相信由于您的width属性位于Window.Resources标记中,因此宽度绑定应如下所示:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM