Wpf 如何获取ControlTemplate元素的属性?

Wpf 如何获取ControlTemplate元素的属性?,wpf,xaml,height,controltemplate,Wpf,Xaml,Height,Controltemplate,我的XAML如下所示: <charting:Chart Name="pieSeries1"> <charting:PieSeries IndependentValuePath="Category" DependentValuePath="Amount" Palette="{StaticResource MyPalette}"> </charting:Pie

我的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>

如何获取
图表区的高度?快速观察显示了
pieSeries1.ChartArea
的值,但此值在运行时不可用。我还尝试了
FindName
,但没有结果


应该很简单吧?

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

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

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

创建一个附加属性(如ChartHeight),并将ChartArea的
实际高度值绑定到它


然后检查代码中的值。

最终我找到了解决方案。不幸的是,@Bubblewrap提供的
OnApplyTemplate
解决方案无法工作

  • 我给我的
    制图:PieSeries
    起了个名字,例如
    pie
  • 在代码隐藏中,我可以通过
    pie.Parent作为EdgePanel
    访问
    ChartArea

您试图从何处获取价值?在代码隐藏中,还是在XAML中?