将Stacked column series chart绑定到Series的问题

时间:2011-03-03 18:47:13

标签: wpf binding wpftoolkit charts

我将Stacked Column Series绑定到我的图表时遇到了很大的问题。

我有 我的ViewModel中的public ObservableCollection Series属性可以通过多种方式尝试,但它仍无效。

这是来自ViewModel准备系列的代码:  private void drawChart()         {             this.Series.Clear();

        var dataValues = new List<List<SimpleDataValue>>();

        int wartoscNiezalezna = 1;

        for (int i = 0; i < 2; i++)
        {
            dataValues.Add(new List<SimpleDataValue>());
        }

        foreach (var item in myCollection)
        {
            var param = someparam;

            dataValues[0].Add(new SimpleDataValue { IndependentValue = "Czujnik " + wartoscNiezalezna, DependentValue = 100 });
            //czerwone
            dataValues[1].Add(new SimpleDataValue { IndependentValue = "" + wartoscNiezalezna, DependentValue = 200 });

            wartoscNiezalezna++;

        }
        var stackedSeries = Activator.CreateInstance(typeof(StackedColumnSeries)) as DefinitionSeries;

        int itemnr=0;
        foreach (var item in dataValues)
        {
            var definicja = new SeriesDefinition();
            if(itemnr==0)
            definicja.Title = "Stan 1";
            else
                definicja.Title = "Stan 2";
            definicja.DependentValuePath = "DependentValue";
            definicja.IndependentValuePath = "IndependentValue";
            definicja.ToolTip = "asdas";
            definicja.ItemsSource = item;
            stackedSeries.SeriesDefinitions.Add(definicja);
            itemnr++;
        }
       Series.Add(stackedSeries);
    }

我无法将其绑定到:

    <charting:Chart x:Name="MyChart"  Padding="10,10,10,10">
        <charting:Chart.Series>
            <charting:StackedColumnSeries>
                <charting:SeriesDefinition ItemsSource="{Binding Series}" DependentValuePath="DependentValue" IndependentValuePath="IndependentValue">
                </charting:SeriesDefinition>
            </charting:StackedColumnSeries>
        </charting:Chart.Series>
    </charting:Chart>

我正在尝试使用SeriesDefinitions Collection和其他人。 我将非常感谢一些帮助。

2 个答案:

答案 0 :(得分:0)

我希望我已经回答了你的问题there

无论如何,我在这里发布了答案的第二部分:

<强> MainWindow.xaml

<charting:Chart x:Name="MyChart"  Padding="10,10,10,10">
    <charting:Chart.Series>
        <charting:StackedColumnSeries>
            <charting:SeriesDefinition Title="Stan 1" ItemsSource="{Binding FirstCollection}" DependentValuePath="DependentValue" IndependentValuePath="IndependentValue" />
            <charting:SeriesDefinition Title="Stan 2" ItemsSource="{Binding SecondCollection}" DependentValuePath="DependentValue" IndependentValuePath="IndependentValue" />
        </charting:StackedColumnSeries>
    </charting:Chart.Series>
</charting:Chart>

<强> MainWindow.xaml.cs

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
        this.MyChart.DataContext = new ChartModel
        {
            FirstCollection = Enumerable.Range(1, 10).Select(i => new SimpleDataValue { IndependentValue = "Czujnik " + i, DependentValue = 100 }).ToList(),
            SecondCollection = Enumerable.Range(1, 10).Select(i => new SimpleDataValue { IndependentValue = "" + i, DependentValue = 200 }).ToList()
        };

    }
}

public class SimpleDataValue
{
    public string IndependentValue { get; set; }
    public int DependentValue { get; set; }
}

public class ChartModel
{
    public List<SimpleDataValue> FirstCollection { get; set; }
    public List<SimpleDataValue> SecondCollection { get; set; }
}

答案 1 :(得分:0)

我不确定语法,但逻辑应如下所示:

视图模型

public class GraphItem    {
    public string IndependentValue { get; set; }
    public int DependentValue1 { get; set; }
    public int DependentValue2 { get; set; }
}

public class ChartViewModel
{
    private List<GraphItem> itemCollection;
    public List<GraphItem> ItemCollection 
    { 
      get { return itemCollection;}
      set { 
            itemCollection=value;
            OnPropertyChanged("ItemCollection");

            }
    }

    public ChartViewModel()
    {
       //Bind ItemCollection 
    }


}

的Xaml:

<charting:Chart x:Name="MyChart"  Padding="10,10,10,10" DataContext={Binding ItemCollection}">
    <charting:Chart.Series>
        <charting:StackedColumnSeries>
            <charting:SeriesDefinition Title="Stan 1" ItemsSource="{Binding}" DependentValuePath="DependentValue1" IndependentValuePath="IndependentValue" />
            <charting:SeriesDefinition Title="Stan 2" ItemsSource="{Binding}" DependentValuePath="DependentValue2" IndependentValuePath="IndependentValue" />
        </charting:StackedColumnSeries>
    </charting:Chart.Series>
</charting:Chart>

愿这有帮助。