1

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

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

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

非常感谢!

4

2 回答 2

1

我认为这仅仅是因为您的数据上下文未设置在您拥有属性“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 以允许属性通知绑定其值已更改,因此绑定将更新目标值。

对不起我的英语不好

于 2012-12-19T22:28:22.270 回答
0

这是我在 C# 中为线条动态重置图表的代码。只需替换并消除不需要的行LineSeriesColumnSeries也将DataContext名称替换为您的名称,样式是我在 XAML windows.resource 中的,因此您可以将其删除,依此类推。也许这将帮助您启动 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);
}
于 2015-09-23T14:39:30.483 回答