Wpf 将堆叠柱系列图表绑定到系列时出现问题

Wpf 将堆叠柱系列图表绑定到系列时出现问题,wpf,binding,wpftoolkit,charts,Wpf,Binding,Wpftoolkit,Charts,我在将堆叠列序列绑定到图表时遇到了一个大问题 我有 我的ViewModel中的PublicObservableCollection系列属性,并尝试了多种方法,但它仍然不起作用 这是从ViewModel到准备系列的代码: 私人虚空图纸() { this.Series.Clear() var dataValues=new List(); int wartoscNiezalezna=1; 对于(int i=0;inew SimpleDataValue{IndependentValue=“Czujnik

我在将堆叠列序列绑定到图表时遇到了一个大问题

我有 我的ViewModel中的PublicObservableCollection系列属性,并尝试了多种方法,但它仍然不起作用

这是从ViewModel到准备系列的代码: 私人虚空图纸() { this.Series.Clear()

var dataValues=new List();
int wartoscNiezalezna=1;
对于(int i=0;i<2;i++)
{
添加(新列表());
}
foreach(myCollection中的var项)
{
var param=someparam;
数据值[0]。添加(新的SimpleDataValue{IndependentValue=“Czujnik”+Wartosniezalezna,DependentValue=100});
//沙文
数据值[1]。添加(新的SimpleDataValue{IndependentValue=”“+Wartosniezalezna,DependentValue=200});
瓦托斯尼扎勒兹纳++;
}
var stackedSeries=Activator.CreateInstance(typeof(StackedColumnSeries))作为DefinitionSeries;
int itemnr=0;
foreach(数据值中的变量项)
{
var definicja=新系列定义();
如果(itemnr==0)
definicja.Title=“斯坦1”;
其他的
definicja.Title=“斯坦2”;
definicja.DependentValue路径=“DependentValue”;
definicja.IndependentValuePath=“IndependentValue”;
definicja.ToolTip=“asdas”;
definicja.ItemsSource=项目;
stackedSeries.SeriesDefinitions.Add(定义JA);
itemnr++;
}
系列。添加(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>

我正在尝试系列定义集合和其他。
我将非常感谢您的帮助。

我希望我已经回答了您的问题

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

主窗口.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; }
}
public部分类主页面:UserControl
{
公共主页()
{
初始化组件();
this.MyChart.DataContext=新图表模型
{
FirstCollection=Enumerable.Range(1,10)。选择(i=>new SimpleDataValue{IndependentValue=“Czujnik”+i,DependentValue=100})。ToList(),
SecondCollection=Enumerable.Range(1,10)。选择(i=>new SimpleDataValue{IndependentValue=”“+i,DependentValue=200})。ToList()
};
}
}
公共类SimpleDataValue
{
公共字符串独立值{get;set;}
public int DependentValue{get;set;}
}
公共类图表模型
{
公共列表第一个集合{get;set;}
公共列表第二集合{get;set;}
}

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

视图模型

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 
    }


}
公共类石墨{
公共字符串独立值{get;set;}
public int DependentValue1{get;set;}
public int DependentValue2{get;set;}
}
公共类图表视图模型
{
私人清单项目收集;
公共列表项集合
{ 
获取{return itemCollection;}
集合{
itemCollection=值;
OnPropertyChanged(“ItemCollection”);
}
}
公共图表视图模型()
{
//绑定项集合
}
}
Xaml:


我做了一个重新标记,下次使用“wpf”标记,因为它有更多的追随者,而且一个问题有更好的机会被回答。
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 
    }


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