【发布时间】:2023-03-11 20:16:02
【问题描述】:
使用实时图表 0.9.7 - 和 .NET 4.5
我正在尝试通过代码将分隔符添加到轴并在代码中绑定分隔符步长值,因为我在运行时动态地将新系列添加到笛卡尔图。分隔符会根据数据集的大小而变化。
这是我的代码:
public partial class PlottingTool : UserControl, INotifyPropertyChanged
{
public static SeriesCollection SeriesCollection { get; set; }
#region LineSeries1Specifics
private double _lineSeries1XAxisSeparatorStep;
// Separator for x-axis
public double LineSeries1XAxisSeparatorStep // Bind the separator for the x-axis to this.
{
get
{
return _lineSeries1XAxisSeparatorStep;
}
set
{
_lineSeries1XAxisSeparatorStep = value;
OnPropertyChanged("LineSeries1AxisSeparatorStep");
}
}
#endregion
public PlottingTool()
{
InitializeComponent();
// Setup Chart
SetupChart();
// DataContext for the liveChart
DataContext = this;
}
private void SetupChart()
{
// Create an empty series collection.
SeriesCollection = new SeriesCollection();
// Setup the axis for the first chart
ChartFile.AxisX.Add(new Axis
{
Title = "Time",
Unit = TimeSpan.FromSeconds(1).Seconds,
Separator = new LiveCharts.Wpf.Separator
{
IsEnabled = true
},
DisableAnimations = true
});
ChartFile.AxisY.Add(new Axis
{
Unit = 1,
DisableAnimations = true
});
// Bind the series1 separator to the x-axis for the first chart
Binding xAxisSeparatorBinding = new Binding();
xAxisSeparatorBinding.Source = this;
xAxisSeparatorBinding.Path = new PropertyPath("LineSeries1XAxisSeparatorStep");
xAxisSeparatorBinding.Mode = BindingMode.OneWay;
xAxisSeparatorBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
BindingOperations.SetBinding(ChartFile.AxisX[0].Separator, LiveCharts.Wpf.Separator.StepProperty, xAxisSeparatorBinding);
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Raises this object's PropertyChanged event.
/// </summary>
/// <param name="propertyName">The property that has a new value.</param>
protected void OnPropertyChanged([CallerMemberName]string propertyName = null)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
#endregion
}
XAML 代码:
<UserControl x:Class="DataAnalyzer.Controls.PlottingTool"
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:local="clr-namespace:DataAnalyzer.Controls"
xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<lvc:CartesianChart Name="ChartFile" Series="{Binding SeriesCollection}" Grid.Row="0" LegendLocation="Top" DisableAnimations="true" Hoverable="false" DataTooltip="{x:Null}" Margin="10">
</lvc:CartesianChart>
</Grid>
为分隔符“step”添加绑定会导致绘图不显示并且 UI 锁定,但没有任何崩溃,并且 Visual Studio 不会提供任何导致错误的反馈。我想知道为什么这不起作用——因为它似乎应该这样做。我已经使用类似的方法为其他项目(如标题)设置了绑定,并且效果很好。
绑定正在更新值,因为我可以通过方法跟踪它的进度。这是绑定的实际分配不起作用。
谢谢...
【问题讨论】:
标签: c# wpf livecharts