This is a migrated thread and some comments may be shown as answers.

Adding custom controls to radchart Template, then conditionally hiding/showing new control

1 Answer 88 Views
Chart
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Scott
Top achievements
Rank 1
Scott asked on 23 Mar 2012, 10:26 PM
Hey all - 

I've created "Utility Bar" for my rad charts by editing the ControlTemplate of the RadChart (it's a simple stackpanel with a few hyperlinkbuttons, "email", "edit", "export", "bookmark"), placed between the chart title & the dock panel that holds the chart area & legend: 

<ControlTemplate TargetType="telerik:RadChart">
    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
        <telerik:ScalePanel MinFullHeight="{TemplateBinding MinFullHeight}" MinFullWidth="{TemplateBinding MinFullWidth}">
            <Grid Background="{TemplateBinding Background}" Margin="{TemplateBinding Padding}">
                <Grid x:Name="PART_DefaultLayoutGrid">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <ContentPresenter x:Name="PART_DefaultChartTitleContainer"/>
                    <StackPanel x:Name="UtilityBar" Orientation="Horizontal" Grid.Row="1">
                            <HyperlinkButton Content="Email" />
                            <TextBlock Text="|" />
                            <HyperlinkButton Content="Bookmark" />
                            <TextBlock Text="|" />
                            <HyperlinkButton Content="Edit" />
                            <TextBlock Text="|" />
                            <HyperlinkButton Content="Export" />
                        </StackPanel>
                    <telerik:RadDockPanel LastChildFill="True" Grid.Row="2">
                        <ContentPresenter x:Name="PART_DefaultChartLegendContainer" telerik:RadDockPanel.Dock="{Binding DefaultView.ChartLegendPosition, RelativeSource={RelativeSource TemplatedParent}}"/>
                        <ContentPresenter x:Name="PART_DefaultChartAreaContainer"/>
                    </telerik:RadDockPanel>
                </Grid>
                <ContentPresenter/>
            </Grid>
        </telerik:ScalePanel>
    </Border>
</ControlTemplate>

I am creating the charts in code behind. I am able to see this Utility Bar no problem when I load my app. I would, however, like to conditionally hide this Utility Bar for certain charts on my dashboard. 

Any idea how this can be done? 

1 Answer, 1 is accepted

Sort by
0
Scott
Top achievements
Rank 1
answered on 25 Mar 2012, 07:48 AM
Duh, same way you'd do it normally. Give it a name, then iterate through the children of the radChart & conditionally hide. Used this FindVisualChildByName class from here

public static T FindVisualChildByName<T>(DependencyObject parent, string name) where T : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);
                string controlName = child.GetValue(Control.NameProperty) as string;
                if (controlName == name)
                {
                    return child as T;
                }
                else
                {
                    T result = FindVisualChildByName<T>(child, name);
                    if (result != null)
                        return result;
                }
            }
            return null;
        }

Then said: 

var utilityBar = FindVisualChildByName<Views.Demo.UtilityBar>(parent, "utilityBar");
if(insert logic here) {
    utilityBar.Visibility = System.Windows.Visibility.Collapsed;
}
Tags
Chart
Asked by
Scott
Top achievements
Rank 1
Answers by
Scott
Top achievements
Rank 1
Share this question
or