Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8444901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:31:07+00:00 2026-06-10T09:31:07+00:00

I have a chart with 4x LineSeries. I defined two different styles for representing

  • 0

I have a chart with 4x LineSeries. I defined two different styles for representing the lines, and I want to be able to change the style applied to a particular LineSeries dynamically (based, for example, on the user tapping a button, etc).

I cannot seem to work out how to update the style from c#. Any help appreciated!

I have tried:

lineChartMood.PolylineStyle = this.Resources.PolylineStyle2 as Style;

but this returns a Null exception.

Here is the XAML for the page, including the style definitions:

    <phone:PhoneApplicationPage 
    x:Class="Bhutaan.ChartingTest"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    xmlns:local="clr-namespace:Bhutaan"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
    shell:SystemTray.IsVisible="True">

    <phone:PhoneApplicationPage.Resources>
        <Style x:Key="PolylineStyle" TargetType="Polyline">
            <Setter Property="StrokeThickness" Value="5"/>
        </Style>
        <Style x:Key="PolylineStyle2" TargetType="Polyline">
            <Setter Property="StrokeThickness" Value="1"/>
        </Style>
    </phone:PhoneApplicationPage.Resources>


    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="TEST" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="added" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,-0,12,0">
            <ScrollViewer>
                <StackPanel>
                    <TextBlock Text="Great! That's been saved." FontSize="30" Margin="0,0,0,0"/>
                    <!-- Chart  -->
                    <charting:Chart
                        x:Name="myChart"
                        Margin="0,20,0,0"
                        Height="350"
                        Style="{StaticResource PhoneChartStyle}"
                        Template="{StaticResource PhoneChartPortraitTemplate}">

                        <!-- Series -->
                        <charting:LineSeries
                            x:Name="lineChartMood"
                            Title="Mood"
                            ItemsSource="{Binding}"
                            DependentValuePath="MoodValue"
                            IndependentValuePath="Timestamp"
                            PolylineStyle="{StaticResource PolylineStyle}" >

                            <charting:LineSeries.LegendItemStyle>
                                <Style TargetType="charting:LegendItem">
                                    <Setter Property="Margin" Value="5 0 5 0"/>
                                </Style>
                            </charting:LineSeries.LegendItemStyle>
                        </charting:LineSeries>
                        <!-- Series -->
                        <charting:LineSeries
                            Title="Energy"
                            ItemsSource="{Binding}"
                            DependentValuePath="EnergyValue"
                            IndependentValuePath="Timestamp">
                            <charting:LineSeries.LegendItemStyle>
                                <Style TargetType="charting:LegendItem">
                                    <Setter Property="Margin" Value="5 0 5 0"/>
                                </Style>
                            </charting:LineSeries.LegendItemStyle>
                        </charting:LineSeries>
                        <!-- Series -->
                        <charting:LineSeries
                            Title="Mental"
                            ItemsSource="{Binding}"
                            DependentValuePath="MentalValue"
                            IndependentValuePath="Timestamp">
                            <charting:LineSeries.LegendItemStyle>
                                <Style TargetType="charting:LegendItem">
                                    <Setter Property="Margin" Value="5 0 5 0"/>
                                </Style>
                            </charting:LineSeries.LegendItemStyle>
                        </charting:LineSeries>
                        <!-- Series -->
                        <charting:LineSeries
                            Title="Hunger"
                            ItemsSource="{Binding}"
                            DependentValuePath="HungerValue"
                            IndependentValuePath="Timestamp">
                            <charting:LineSeries.LegendItemStyle>
                                <Style TargetType="charting:LegendItem">
                                    <Setter Property="Margin" Value="5 0 5 0"/>
                                </Style>
                            </charting:LineSeries.LegendItemStyle>
                        </charting:LineSeries>
                    </charting:Chart>

                </StackPanel>
            </ScrollViewer>
        </Grid>
    </Grid>


</phone:PhoneApplicationPage>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:31:09+00:00Added an answer on June 10, 2026 at 9:31 am

    I tested your code and found the cause of the problem.

    You get the NullReferenceException because the field this.lineChartMood isn’t set by the application for some unknown reason.

    You have to obtain this line series object by yourself. There are two possible ways to get it:

    var lineSeriesWay1 = this.FindName("lineChartMood");
    var lineSeriesWay2 = myChart.Series.OfType<LineSeries>().First(ls => ls.Name == "lineChartMood");
    

    But I would recommend to set the field this.lineSeriesMood explicitly in the constructor, like this:

    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            // because the code 'this.FindName("lineChartMood")' doesn't work in the constructor, I'll use the following line:
            this.lineChartMood = this.myChart.Series.OfType<LineSeries>().First(ls => ls.Name == "lineChartMood");
    
            // other code
        }
    
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.lineChartMood.PolylineStyle = (Style)this.Resources["PolylineStyle2"];
            this.lineChartMood.Refresh(); // you should call this method so that the style is applied
        }
    }
    

    Then you will be able to reference to your series without exceptions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a chart (code to replicate will be below) that has two lines
I have a chart typeo of pie and need to change the colors that
I have a Visiblox chart with multiple Y-axes, different units on each axis. The
I have a chart and I want the user to see the values when
Here is my question: I have a hybrid chart that uses bars and lines
I have multiple line series in a chart. Chart lines are drawn first and
I have some chart rendering tasks that I want to cover with tests. I
I have a Visiblox chart showing several LineSeries . I would like to be
I have chart.png with data in it that I would like to put a
I have a chart and I need to clear it in order to populate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.