为什么LiveCharts SeriesCollection无法在ItemsControl DataTemplate内部工作?

时间:2019-01-14 14:09:44

标签: wpf itemscontrol livecharts

编辑:我的图表可以工作,但是标题中没有回答问题。 我将xaml从下面的内容更改为以下内容。

<chartWPF:CartesianChart Height="{Binding Path=MainVM.Scaler.HalfScreenLTHeight}" Width="{Binding Path=MainVM.Scaler.HalfScreenLTWidth}" Foreground="Black">
    <chartWPF:CartesianChart.Series >
        <chartWPF:LineSeries Values="{Binding Path=DBWindow.HourGross}" StrokeThickness="{Binding Path=MainVM.Scaler.StrokeSize}" Title="Target" PointGeometrySize = "{Binding Path=MainVM.Scaler.LineDotSize}" PointForeground = "DeepSkyBlue" Stroke="DeepSkyBlue" Fill="Transparent" LineSmoothness="0" />
        <chartWPF:StackedColumnSeries Values="{Binding Path=DBWindow.HourCounts}" StrokeThickness="0" StrokeDashArray="0" Title="Good Parts" Fill="Green" PointGeometry="{x:Null}" />
        <chartWPF:StackedColumnSeries Values="{Binding Path=DBWindow.HourScrap}" StrokeThickness="0" StrokeDashArray="0" Title="Scrap Parts" Fill="Red" PointGeometry="{x:Null}" />      
    </chartWPF:CartesianChart.Series>
    <chartWPF:CartesianChart.AxisX>
        <chartWPF:Axis Title="Hourly Counts" Labels="{Binding Path=DBWindow.Hours}" ShowLabels="True" Typography.Kerning="False" Foreground="Black" FontSize="{Binding Path=MainVM.Scaler.ScrapButtonFontSize3}" LabelFormatter="{Binding Path=DBWindow.HoursLabels}" MinValue="0">
            <chartWPF:Axis.Separator>
                <chartWPF:Separator Step="1"></chartWPF:Separator>
            </chartWPF:Axis.Separator>
        </chartWPF:Axis>
    </chartWPF:CartesianChart.AxisX>
    <chartWPF:CartesianChart.AxisY>
        <chartWPF:Axis Title="Parts" Foreground="Black" FontSize="{Binding Path=MainVM.Scaler.ScrapButtonFontSize3}" MinValue="0">
        </chartWPF:Axis>
    </chartWPF:CartesianChart.AxisY>
</chartWPF:CartesianChart>

原始帖子: 我正在尝试从在屏幕上显示一台计算机的图表转变为一次显示四台计算机。我正在使用LiveCharts.WPF NuGet。我正在将WPF与MVVM一起使用。在图表上,每个小时的好零件和报废零件必须有一个堆积的条形,其线形与这些小时的预期总值相同。

我已经尝试使用itemscontrol和设置的4个独立图表来查看可观察集合“ MachList [0]”中的特定机器

如果我更改

chartWPF:CartesianChart Series="{Binding Path=DBWindow.SeriesHoursCollection}"

chartWPF:CartesianChart Series="{Binding Path=MainVM.SelectedMach.DBWindow.SeriesHoursCollection}"

该图仅在第四张图表上显示当前选择的计算机,无论我选择了哪台计算机,尽管该图表应该在所有图表上。我可以向该计算机添加计数,并且图形更新不会崩溃。

XAML:

<ItemsControl ItemsSource="{Binding Path=MachList}" >
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Horizontal" HorizontalAlignment="Center"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <chartWPF:CartesianChart Series="{Binding Path=DBWindow.SeriesHoursCollection}" LegendLocation="Bottom" 
                                                    Height="{Binding Path=MainVM.Scaler.HalfScreenLTHeight}" Width="{Binding Path=MainVM.Scaler.HalfScreenLTWidth}" Foreground="Black">
                    <chartWPF:CartesianChart.AxisX>
                        <chartWPF:Axis Title="Hourly Counts" 
                                                            Labels="{Binding Path=DBWindow.Hours}" ShowLabels="True" Typography.Kerning="False"
                                                            Foreground="Black" FontSize="{Binding Path=MainVM.Scaler.ScrapButtonFontSize3}"
                                                            LabelFormatter="{Binding Path=DBWindow.HoursLabels}" MinValue="0">
                             <chartWPF:Axis.Separator>
                                 <chartWPF:Separator Step="1"></chartWPF:Separator>                                                        
                             </chartWPF:Axis.Separator>
                         </chartWPF:Axis>
                     </chartWPF:CartesianChart.AxisX>
                     <chartWPF:CartesianChart.AxisY>
                          <chartWPF:Axis Title="Parts" Foreground="Black" FontSize="{Binding Path=MainVM.Scaler.ScrapButtonFontSize3}" MinValue="0">
                          </chartWPF:Axis>            
                     </chartWPF:CartesianChart.AxisY>
                 </chartWPF:CartesianChart>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

DBWindow:

public SeriesCollection SeriesHoursCollection
{
    get { return _seriesHoursCollection; }
    set
    {
        _seriesHoursCollection = value;
        OnPropertyChanged("");
    }
}
public Func<double, string> HoursLabels
{
        get { return _hLables; }
        set
        {
           _hLables = value;
           OnPropertyChanged();
        }
}
public ChartValues<double> HourCounts
{
        get { return _hourCouts; }
        set
        {
           _hourCouts = value;
           OnPropertyChanged();
        }
}
public ChartValues<double> HourScraps
{
        get { return _hourScraps; }
        set
        {
           _hourScraps = value;
           OnPropertyChanged();
        }
}
public ChartValues<double> HourGross
{
        get { return _hourGross; }
        set
        {
            _hourGross = value;
            OnPropertyChanged();
        }
}
public List<string> Hours
{
            get { return _hours; }
            set
            {
                _hours = value;
                OnPropertyChanged();


   }
}
//Function is called to update the charts when counts are changed for a machine
public void HourStackBars()
{
        if (SeriesHoursCollection.Chart != null)
        { SeriesHoursCollection.Clear(); }

        Hours = new List<string>();
        HourCounts = new ChartValues<double>();
        HourScraps = new ChartValues<double>();
        HourGross = new ChartValues<double>();
        var maxGross = 0;
        var hour = 1;

        for (int i=0; i< _machVM.HourCountGraph.Count; i++)
        {
            if(i == 0) { maxGross = _machVM.HourCountGraph[i].Gross; }
            HourCounts.Add(_machVM.HourCountGraph[i].GoodParts);
            HourScraps.Add(_machVM.HourCountGraph[i].ScrapParts);
            HourGross.Add(_machVM.HourCountGraph[i].Gross);
            Hours.Add(Convert.ToString(_machVM.HourCountGraph[i].Hour));
            hour++;
        }


        for (int t = hour; t <= _machVM.MainVM.ShiftHourCount; t++)
        {
            if (_machVM.RunningPart != "")
            {
                maxGross = Convert.ToInt32(_machVM.MainVM.SelectedPart.PartGross);
                HourGross.Add(maxGross);
            }
            Hours.Add(Convert.ToString(hour));
            hour++;
        }


        if (HourCounts.Count > 0 && HourGross.Count > 0)
        {

            SeriesHoursCollection = new SeriesCollection
            {
                new LineSeries
                {
                    Title = "Target",
                    Values = HourGross,
                    Stroke = Brushes.DeepSkyBlue,
                    StrokeThickness = _machVM.MainVM.Scaler.StrokeSize,
                    PointGeometrySize = _machVM.MainVM.Scaler.LineDotSize,
                    FontSize = _machVM.MainVM.Scaler.ReportHeadFontSize,
                    PointForeground = Brushes.DeepSkyBlue,
                    UseLayoutRounding = false,
                    Fill = Brushes.Transparent
                },
                new StackedColumnSeries
                {
                    Title = "Good Parts",
                    Values = HourCounts,
                    DataLabels = true,
                    Fill = Brushes.Green,
                    FontSize = _machVM.MainVM.Scaler.ReportHeadFontSize,
                    UseLayoutRounding = false
                },
                new StackedColumnSeries
                {
                    Title = "Scrap Parts",
                    Values = HourScraps,
                    DataLabels = true,
                    Fill = Brushes.Red,
                    FontSize = _machVM.MainVM.Scaler.ReportHeadFontSize,
                    UseLayoutRounding = false
                }
            };
        }
        else
        {
            SeriesHoursCollection = new SeriesCollection();
        }

        HoursLabels = val => val.ToString("P");
}

我想看到的是屏幕上的4个图形,当我向计算机添加计数时,该计算机的图形将更新以反映此添加。现在发生的是程序崩溃。

在单个图表,单个计算机环境中,一切都可以正常工作。

0 个答案:

没有答案