简体   繁体   中英

Binding LinearAxis Interval WPF Toolkit Chart

I have a chartingToolKit in my XAML file, and I want to set the interval of the LinearAxis dynamically instead of static in my XAML file. This is how I do it now:

<chartingToolkit:ColumnSeries.DependentRangeAxis>
    <chartingToolkit:LinearAxis FontSize="15" Foreground="Black" Interval="1"  Minimum="0" Orientation="Y" ShowGridLines="False" />
</chartingToolkit:ColumnSeries.DependentRangeAxis>

I tried to do it this way with binding:

<chartingToolkit:ColumnSeries.DependentRangeAxis>
    <chartingToolkit:LinearAxis FontSize="15" Foreground="Black" Interval="{Binding ChartingInterval}"  Minimum="0" Orientation="Y" ShowGridLines="False" />
</chartingToolkit:ColumnSeries.DependentRangeAxis>

The binding reference to the property ChartingInterval in my .cs file, like this:

public int ChartingInterval
{
    get
    {
        //Should contain more logic, obvious.
        return 1;    
    } 
} 

But this doesn't seems to work properly. How can I accomplish this?

Many thanks!

I think this is simply because your datacontext isn't set at the object on which you have your property "ChartingInterval".

You should simply set it as follow :

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:chartingToolkit="..."
        Title="MainWindow" Height="350" Width="525"
        DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">
    ...
    <chartingToolkit:ColumnSeries.DependentRangeAxis>
        <chartingToolkit:LinearAxis FontSize="15" Foreground="Black" Interval="{Binding ChartingInterval}"  Minimum="0" Orientation="Y" ShowGridLines="False" />
    </chartingToolkit:ColumnSeries.DependentRangeAxis>
    ...
</Window>

You should implement INotifyPropertyChanged to allow the property to notify the binding that its value changed, so the binding will update the target value.

Sorry for my english

Here is my code to reset my chart dynamically in C# for a lineseries. Just replace LineSeries by ColumnSeries and eliminate the lines that you do not need. Replace also the DataContext name with yours, the Style is mine in the XAML windows.resource so you can eliminate it, and so on. Maybe this will help you to start the C# code.

//dynamically recreate the chart series1
private void AddSeries()
{
    var series1 = new LineSeries();
    series1.SetBinding(LineSeries.ItemsSourceProperty, new Binding());
    series1.DataContext = Power;
    series1.DependentValueBinding = new Binding("Value");
    series1.IndependentValueBinding = new Binding("Key");
    series1.Style = (Style)this.Resources["LineSeriesStyle1"];

    //set initial values:
    LinearAxis independentaxis = new LinearAxis();
    independentaxis.Orientation = AxisOrientation.X;
    independentaxis.ShowGridLines = true;
    independentaxis.Maximum = 60;
    independentaxis.Minimum = 0;
    independentaxis.Title = "Time";
    independentaxis.ShowGridLines = true;
    series1.IndependentAxis = independentaxis;

    //set initial values:
    LinearAxis dependentaxis = new LinearAxis();
    dependentaxis.Orientation = AxisOrientation.Y;
    dependentaxis.ShowGridLines = true;
    dependentaxis.Maximum = 600;
    dependentaxis.Minimum = 0;
    dependentaxis.Title = "Force(n)";
    dependentaxis.ShowGridLines = true;
    series1.DependentRangeAxis = dependentaxis;
    chart1.Series.Add(series1);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM