绑定LinearAxis Interval WPF工具包图表

时间:2012-12-19 22:10:43

标签: c# wpf

我的XAML文件中有一个chartingToolKit,我想在XAML文件中动态设置LinearAxis的间隔而不是静态。这就是我现在这样做的方式:

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

我尝试用绑定方式这样做:

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

我的.cs文件中对ChartingInterval属性的绑定引用,如下所示:

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

但这似乎不能正常工作。我怎么能做到这一点?

非常感谢!

2 个答案:

答案 0 :(得分:1)

我认为这只是因为您的datacontext未设置在您拥有“ChartingInterval”属性的对象上。

您只需将其设置如下:

<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>

您应该实现INotifyPropertyChanged以允许属性通知绑定其值已更改,因此绑定将更新目标值。

抱歉我的英文

答案 1 :(得分:0)

这是我在C#中为lineseries动态重置我的图表的代码。只需用ColumnSeries替换LineSeries并消除不需要的行。将你的DataContext名称替换为你的,在XAML windows.resource中使用Style是我的,所以你可以消除它......依此类推......也许这将有助于你启动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);


    }