遇到的问题,在主窗口中调用用户控件的 AxisYMax,AxisYMin 属性,图表没有发生对应的更新,并且主窗口的DataContext已被占用,这个该如何解决呀
用户控件:LineChart_UserControl的xaml代码:
<UserControl x:Class="IZeroVisionVM.LineChart_UserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
xmlns:local="clr-namespace:IZeroVisionVM"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800" Loaded="UserControl_Loaded">
<Grid>
<Grid>
<lvc:CartesianChart Series="{Binding LineSeriesCollection}" LegendLocation="Right">
<lvc:CartesianChart.AxisX>
<lvc:Axis Labels="{Binding Labels}" FontSize="14">
<lvc:Axis.Separator>
<lvc:Separator Step="1" />
</lvc:Axis.Separator>
</lvc:Axis>
</lvc:CartesianChart.AxisX>
<lvc:CartesianChart.AxisY>
<lvc:Axis LabelFormatter="{Binding CustomFormatterY}" MaxValue="{Binding AxisYMax}" MinValue="{Binding AxisYMin}" Position="LeftBottom">
</lvc:Axis>
</lvc:CartesianChart.AxisY>
</lvc:CartesianChart>
</Grid>
</Grid>
</UserControl>
用户控件后台代码:
using LiveCharts;
using LiveCharts.Wpf;
using Rapid.UI.IZVKernal;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace IZeroVisionVM
{
/// <summary>
/// LeftOutSide_UserControl.xaml 的交互逻辑
/// </summary>
public partial class LineChart_UserControl : UserControl , INotifyPropertyChanged
{
public List<string> Labels { get; set; }
public SeriesCollection LineSeriesCollection { get; set; }
private double axisYMax;
public double AxisYMax
{
get { return axisYMax; }
set { axisYMax = value; this.RaisePropertyChanged("AxisYMax"); }
}
private double axisYMin;
public double AxisYMin
{
get { return axisYMin; }
set { axisYMin = value; this.RaisePropertyChanged("AxisYMin"); }
}
public Func<double, string> CustomFormatterY { get; set; }
public LineChart_UserControl()
{
InitializeComponent();
}
//绑定的X轴数据
public static ChartValues<double> ValueList { get; set; }
public void OnClick(float distance)
{
//向图表中添加数据
ValueList.Add(distance);
//这里如果移除数组,图表曲线会原地起伏,就没有X轴移动的动画效果了
if (ValueList.Count > 11)
{
ValueList.RemoveAt(0);
}
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
Mode();
this.DataContext = this;
}
private string CustomFormattersY(double val)
{
return string.Format("{0} mm", val);
}
public void Mode()
{
Labels = new List<string>();
AxisYMax = 420 + 30;
double max = AxisYMax - 30;
double warningMax = 400;
double guige = 390;
double warningMin = 380;
AxisYMin = 370 - 20;
double min = AxisYMin + 20;
LineChart_UserControl.ValueList = new ChartValues<double>();
LineSeriesCollection = new SeriesCollection
{
new LineSeries
{
Title = $"上限:{max} mm",
Values = new ChartValues<double> { max, max, max, max, max, max, max, max, max, max, max},
Stroke = System.Windows.Media.Brushes.Red,
PointGeometry = null,
Fill = System.Windows.Media.Brushes.LightBlue,
},
new LineSeries
{
Title = $"预警线:{warningMax} mm",
StrokeDashArray = new System.Windows.Media.DoubleCollection {2},
Values = new ChartValues<double> { warningMax, warningMax, warningMax, warningMax, warningMax, warningMax, warningMax, warningMax, warningMax, warningMax, warningMax},
Stroke = System.Windows.Media.Brushes.Blue,
PointGeometry = null,
Fill = System.Windows.Media.Brushes.LightBlue,
},
new LineSeries
{
Title = $"规格:{guige} mm",
Values = new ChartValues<double> { guige, guige, guige, guige, guige, guige, guige, guige, guige, guige, guige},
Stroke = System.Windows.Media.Brushes.Green,
PointGeometry = null,
Fill = System.Windows.Media.Brushes.LightBlue,
},
new LineSeries
{
Title = $"预警线:{warningMin} mm",
StrokeDashArray = new System.Windows.Media.DoubleCollection {2},
Values = new ChartValues<double> { warningMin, warningMin, warningMin, warningMin, warningMin, warningMin, warningMin, warningMin, warningMin, warningMin, warningMin},
Stroke = System.Windows.Media.Brushes.Blue,
PointGeometry = null,
Fill = System.Windows.Media.Brushes.LightBlue,
},
new LineSeries
{
Title = $"下限:{min} mm",
Values = new ChartValues<double> { min, min, min, min, min, min, min, min, min, min, min},
Stroke = System.Windows.Media.Brushes.Red,
PointGeometry = null,
Fill = System.Windows.Media.Brushes.LightBlue,
}
};
CustomFormatterY = CustomFormattersY;
LineSeries lineseries = new LineSeries();
lineseries.Title = "极耳中心距";
//lineseries.Stroke = System.Windows.Media.Brushes.Purple;
lineseries.DataLabels = true;
lineseries.Values = LineChart_UserControl.ValueList;
LineSeriesCollection.Add(lineseries);
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
在MainWindow窗口添加用户控件 LineChart_UserControl
<Grid Grid.Row="0" Grid.Column="2">
<local:LineChart_UserControl></local:LineChart_UserControl>
</Grid>