Cannot change LabelStyle in runtime

4 posts, 1 answers
  1. Jesper
    Jesper avatar
    18 posts
    Member since:
    Jun 2012

    Posted 31 Aug 2012 Link to this post


    In my project I have a need for printing the currently presented chart. I do this by simply rendering the chart into an image in memory but the on-screen colors are not fit for a white (paper) background so I need to change some of them before the rendering starts. 

    The chart is a RadCartesianChart and this is the part of it that declares the Horizontal axis:

                LabelTemplate="{StaticResource _dateTimeLabelTemplate}"
                LabelStyle="{StaticResource HmiChartLabelStyle}"
                LineStroke="{StaticResource HmiForeColor3Brush}"
                <Style TargetType="Rectangle">
                    <Setter Property="Fill" Value="{StaticResource HmiForeColor3Brush}" />

    As a side note I would like to mention that I have also tried the MVVM approach and bound the LabelStyle to the view model but according to this thread that is currently not possible due to a bug that is scheduled to be fixed in a later update.

    So, with the above declaration refering to a static resource I later prepare the chart for hard copy rendering like this:

    private static void prepareChartForRendering(RadCartesianChart chart, out object restoreState)
        var chartState = new ChartState
                               LabelStyle = chart.HorizontalAxis.LabelStyle,
                               PanAndZoomBehavior = chart.Behaviors.FirstOrDefault(child => child is ChartPanAndZoomBehavior) as ChartPanAndZoomBehavior
        chart.HorizontalAxis.LabelStyle = HmiResources.HmiPrintedChartLabelStyle;
        if (chartState.PanAndZoomBehavior != null)
        restoreState = chartState;

    The 'HmiPrintedChartLabelStyle' referred to specifies a black Foreground color but the rendered chart is still rendered with its white labels. While experimenting with the chart's behavior I tried to omit the restoration of the chart LabelStyle after the hard copy rendering and it seems I cannot affect the LabelStyle at all: It's still white, even on screen, after I have changed the LabelStyle to a black font.

    How do I go about changing the LabelStyle at runtime?

  2. Jesper
    Jesper avatar
    18 posts
    Member since:
    Jun 2012

    Posted 31 Aug 2012 Link to this post

    I just realized I posted in the Silverlight branch whereas my project is WPF. I don't know if there's any technincal difference but feel free to move the post to the WPF>ChartView branch.
  3. Answer
    Petar Marchev
    Petar Marchev avatar
    996 posts

    Posted 04 Sep 2012 Link to this post

    Hi Jesper,

    The RadChartView supports export and print for a fully rendered chart. If you change a text box style and you try to export it right away - it just might not work, because a layout pass has not occured.
    I have created a simple project based on your code and explanations. It exports a chart on the click of a button.In this event handler, the LabelStyle property of the horizontal axis is changed, then the chart is exported and then the LabelStyle is set to its original value. Do examine the attached project and see if this is what you are trying to achieve. If not, you can update the project accordingly to demonstrate the issues you are experiencing and open a new support ticket and attach the project to it. 

    All the best,
    Petar Marchev
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  4. Jesper
    Jesper avatar
    18 posts
    Member since:
    Jun 2012

    Posted 12 Sep 2012 Link to this post

    Forcing a layout pass did the trick. Thank you!
Back to Top