我怎样才能获得ControlTemplate元素的属性?

时间:2011-09-30 10:35:39

标签: wpf xaml height controltemplate

我的XAML看起来像这样:

    <charting:Chart
            Name="pieSeries1">
        <charting:PieSeries
            IndependentValuePath="Category" DependentValuePath="Amount"
            Palette="{StaticResource MyPalette}">
        </charting:PieSeries>
        <charting:Chart.Template>
            <ControlTemplate TargetType="charting:Chart">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <datavis:Title Content="{TemplateBinding Title}" Style="{TemplateBinding TitleStyle}" Margin="1"/>
                    <Grid Grid.Row="1" Margin="5,0,5,0">
                        <chartingPrmtvs:EdgePanel x:Name="ChartArea" MinHeight="200" Style="{TemplateBinding ChartAreaStyle}">
                            <Grid Canvas.ZIndex="-1" Style="{TemplateBinding PlotAreaStyle}" />
                            <Border Canvas.ZIndex="10" BorderBrush="#FF919191" BorderThickness="1" />
                        </chartingPrmtvs:EdgePanel>
                    </Grid>
                    <datavis:Legend VerticalAlignment="Top" Grid.Row="2" Header="{TemplateBinding LegendTitle}" Style="{TemplateBinding LegendStyle}" x:Name="Legend"/>
                </Grid>
            </ControlTemplate>
        </charting:Chart.Template>
    </charting:Chart>

如何获得ChartArea的高度?快速监视显示pieSeries1.ChartArea的值,但在运行时不提供此值。我也试过FindName,但没有结果。

应该相当容易吗?

3 个答案:

答案 0 :(得分:2)

您可以在Chart类中添加以下内容:

    private EdgePanel panel;
    public override void OnApplyTemplate()
    {
        panel = (EdgePanel)Template.FindName("ChartArea", this);
    }

何时以及如何获得高度取决于你自己的逻辑需要它。

答案 1 :(得分:0)

创建一个附加属性(比如ChartHeight)并将ChartArea的ActualHeight值绑定到它。

之后只需检查代码中的值。

答案 2 :(得分:0)

最终我找到了解决方案。不幸的是,来自@Bubblewrap的OnApplyTemplate解决方案无效。

  • 我给了charting:PieSeries一个名字,例如pie
  • 在后面的代码中,我可以通过ChartArea
  • 访问pie.Parent as EdgePanel
相关问题