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

2 posts, 0 answers
  1. Scott
    Scott avatar
    4 posts
    Member since:
    Jan 2012

    Posted 23 Mar 2012 Link to this post

    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? 
  2. Scott
    Scott avatar
    4 posts
    Member since:
    Jan 2012

    Posted 25 Mar 2012 Link to this post

    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;
    }
  3. DevCraft banner
Back to Top