0

I have created a usercontrol for livechart and trying to create dependency properties around it. But the dependency property is not getting updated. I googled and found a answer in StackOverflow. so I have been searching around. i followed the steps as mentioned in the answer still i am not able to get the chart updated. CODE:-

<LiveChart:CartesianChart Grid.Row="1" Background="White">
        <LiveChart:CartesianChart.AxisX>
            <LiveChart:Axis Title="{Binding Path=XAxisTitle, ElementName=chartControl, UpdateSourceTrigger=PropertyChanged}" Labels="{Binding Path=XAxisValues, ElementName=chartControl, UpdateSourceTrigger=PropertyChanged}"/>
        </LiveChart:CartesianChart.AxisX>
        <LiveChart:CartesianChart.AxisY>
            <LiveChart:Axis Title="{Binding Path=YAxisTitle, ElementName=chartControl, UpdateSourceTrigger=PropertyChanged}" LabelFormatter="{Binding Path=Formatter, ElementName=chartControl, UpdateSourceTrigger=PropertyChanged}" MinValue="0"></LiveChart:Axis>
        </LiveChart:CartesianChart.AxisY>
        <LiveChart:CartesianChart.Series>
            <LiveChart:LineSeries Values="{Binding Path=SpectrumChartSeries, ElementName=chartControl, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"></LiveChart:LineSeries>
        </LiveChart:CartesianChart.Series>
    </LiveChart:CartesianChart>

I have named the user control as chartControl and used the ElementName. My backend:-

public List<Point> ChartPoints
    {
        get { return (List<Point>)GetValue(ChartPointsProperty); }
        set { SetValue(ChartPointsProperty, value); }
    }

    public static readonly DependencyProperty ChartPointsProperty = DependencyProperty.Register("ChartPoints", typeof(List<Point>), typeof(chartcontrol), new FrameworkPropertyMetadata(new List<Point>(), onChartrPointChange));

    private static void onChartrPointChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var control = d as chartcontrol;
        var points = e.NewValue as List<Point>;
        control.UpdateChart(points);
    }

    private void UpdateChart(List<Point> chartPoints)
    {
        SpectrumChartSeries = new ChartValues<double>();// { 4, 6, 5, 2, 4, 8, 9, 10, 20, 11, 15 };
        SpectrumChartSeries.AddRange(chartPoints.Select(x => x.Y));
        XAxisValues = chartPoints.Select(x => x.X.ToString()).ToList();//new List<string>() { "1", "2", "3", "4", "5" };
        Formatter = value => (value).ToString("N", System.Globalization.CultureInfo.CurrentCulture);
        XAxisTitle = " test title";
        YAxisTitle = "y title";
    }

The changed event is getting hit and the values are updated. but still not reflecting in the chart. so bit confused.

The thing is when i try to Use the WPF tool Snoop and click on the line series, i can see that the chart get updated immediately,

Before click:- Before click

After click:- After click

manu vishwanath
  • 97
  • 1
  • 1
  • 14
  • In UpdateChart you're updating some (internal?) properties (like SpectrumChartSeries etc). Do these properties notify about changes? As a note, it's certainly not necessary to set `UpdateSourceTrigger=PropertyChanged` on their Bindings, because the Bindings aren't TwoWay. – Clemens Jul 24 '18 at 10:47
  • It is written in xaml.cs file so i have not written any notify changes event. should I? as it is code behind. it must update immediately right. – manu vishwanath Jul 24 '18 at 10:55
  • A Binding like `{Binding Path=SpectrumChartSeries, ...}` only updates its target property when the source property fires a change notification. So SpectrumChartSeries must either be a dependency property or its owning class must implement INotifyPropertyChanged and fire the PropertyChanged event in the SpectrumChartSeries setter. – Clemens Jul 24 '18 at 10:58
  • @Clemens I tried adding the INotifyPropertyChanged. still does not work. the propertychanged was not getting hit, googled and found to set datacontext. did that also. still not working. any other approach please – manu vishwanath Jul 25 '18 at 06:02

1 Answers1

0

Posting the answer so that it might help someone like me. I was able to find the answer this blog helped me. as they have mentioned i needed to add the datacontext in the Xaml.

LiveChart:CartesianChart Grid.Row="1" Background="White" DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:LiveChartControl}}">
        <LiveChart:CartesianChart.AxisX>
            <LiveChart:Axis Title="{Binding Path=XAxisTitle, Mode=TwoWay}" Labels="{Binding Path=XAxisValues, Mode=TwoWay}" MinValue="0"/>
        </LiveChart:CartesianChart.AxisX>
        <LiveChart:CartesianChart.AxisY>
            <LiveChart:Axis Title="{Binding Path=YAxisTitle, Mode=TwoWay}" LabelFormatter="{Binding Path=Formatter, Mode=TwoWay}" MinValue="0"></LiveChart:Axis>
        </LiveChart:CartesianChart.AxisY>
        <LiveChart:CartesianChart.Series>
            <LiveChart:LineSeries Values="{Binding Path=ChartPointsInChartValues, Mode=TwoWay}"/>
            <LiveChart:VerticalLineSeries Values="{Binding Path=EnergyLineChartValues , Mode=TwoWay}"/>
        </LiveChart:CartesianChart.Series>
    </LiveChart:CartesianChart>

This started to work now.

manu vishwanath
  • 97
  • 1
  • 1
  • 14