简体   繁体   English

绑定 LinearAxis 间隔 WPF 工具包图表

[英]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.我有一个chartingToolKit在我的XAML文件,我想设置的间隔LinearAxis我的动态而不是静态XAML文件。 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: .cs 文件中对 ChartingInterval 属性的绑定引用,如下所示:

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".我认为这仅仅是因为您的 datacontext 没有设置在您拥有属性“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.您应该实现 INotifyPropertyChanged 以允许属性通知绑定其值已更改,因此绑定将更新目标值。

Sorry for my english对不起我的英语不好

Here is my code to reset my chart dynamically in C# for a lineseries.这是我在 C# 中为线系列动态重置图表的代码。 Just replace LineSeries by ColumnSeries and eliminate the lines that you do not need.只需将LineSeries替换为ColumnSeries并消除您不需要的行。 Replace also the DataContext name with yours, the Style is mine in the XAML windows.resource so you can eliminate it, and so on.还将DataContext名称替换为您的名称,XAML windows.resource 中的样式是我的,因此您可以将其删除,等等。 Maybe this will help you to start the C# code.也许这会帮助您开始 C# 代码。

//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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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