321X
321X

Reputation: 3185

How can I get the property of ControlTemplate element?

My XAML looks like this:

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

How can I get the height of ChartArea? Quick Watch shows the value of pieSeries1.ChartArea but this one is not available on Runtime. I also tried FindName but no result.

Should be fairly easy right?

Upvotes: 1

Views: 258

Answers (3)

Louis Kottmann
Louis Kottmann

Reputation: 16648

Create an attached property (say ChartHeight) and bind the value of ActualHeight of ChartArea to it.

After that just check the value in your code.

Upvotes: 0

321X
321X

Reputation: 3185

Eventually I found the solution. The OnApplyTemplate solution from @Bubblewrap did not work, unfortunately.

  • I gave my charting:PieSeries a name, e.g. pie.
  • In the code behind I was able to access the ChartArea via pie.Parent as EdgePanel.

Upvotes: 0

Bubblewrap
Bubblewrap

Reputation: 7526

You can add the following in your Chart class:

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

When and how to get the height depends on your own logic needs it.

Upvotes: 2

Related Questions