我希望将WPF中每个行系列的值绑定到C#中类型为C#的值的集合。目前,每一行都指定了实数。我如何将它绑定到一个集合?一个例子是将第一行系列绑定到下面的系列集合。非常感谢!
<lvc:CartesianChart>
<lvc:CartesianChart.Series>
<lvc:LineSeries Title="Pres" Values="4,7,2,9,3" Visibility="{Binding PresVisibility, Converter={StaticResource bvc}}"/>
<lvc:LineSeries Title="Temp" Values="6,2,6,3,8" Visibility="{Binding TempVisibility, Converter={StaticResource bvc}}"/>
<lvc:LineSeries Title="Flow" Values="7,2,8,3,9" Visibility="{Binding FlowVisibility, Converter={StaticResource bvc}}"/>
<lvc:LineSeries Title="Control" Values="6,2,6,3,8" Visibility="{Binding ControlVisibility, Converter={StaticResource bvc}}"/>
<lvc:LineSeries Title="Compressor" Values="7,2,8,3,9" Visibility="{Binding CompressorVisibility, Converter={StaticResource bvc}}"/>
</lvc:CartesianChart.Series>
<lvc:CartesianChart.AxisX>
<lvc:Axis Title="Time" Labels="{Binding CurrentLabels}"></lvc:Axis>
</lvc:CartesianChart.AxisX>
<lvc:CartesianChart.AxisY>
<lvc:Axis Name="yAxis" LabelFormatter="{Binding CurrentYFormatter}"></lvc:Axis>
</lvc:CartesianChart.AxisY>
</lvc:CartesianChart>
在C#中:
public SeriesCollection PresSeries { get; set; }
PresSeries[0].Values.Add(ScrewState.GetScrewAll().V101.p);
PresSeries[1].Values.Add(ScrewState.GetScrewAll().V102.p);
PresSeries[2].Values.Add(ScrewState.GetScrewAll().P1.p);
PresSeries[3].Values.Add(ScrewState.GetScrewAll().P2.p);
PresSeries[4].Values.Add(ScrewState.GetScrewAll().P3.p);
发布于 2020-11-26 19:10:55
当您在XAML中定义LineSeries
时,就不必再定义SeriesCollection
了(您只是在XAML中这样做-- <CartesianChart.Series>
就是SeriesCollection
)。在ChartValues
中定义实际的C#源代码集合就足够了(例如,在视图模型中):
MainWindow.xaml
<lvc:CartesianChart x:Name="CartesianChart">
<lvc:CartesianChart.DataContext>
<local:LiveChartsModel />
</lvc:CartesianChart.DataContext>
<lvc:CartesianChart.Series>
<lvc:LineSeries Title="Sine Graph"
Values="{Binding SineGraphValues}"
Configuration="{Binding DataMapper}"/>
</lvc:CartesianChart.Series>
</lvc:CartesianChart>
LiveChartsModel.cs
public class LiveChartsModel : INotifyPropertyChanged
{
public LiveChartsModel()
{
// Initialize the binding source
this.SineGraphValues = new ChartValues<ObservablePoint>();
// Plot a sine graph
for (double x = 0; x <= 360; x++)
{
var point = new ObservablePoint()
{
X = x,
Y = Math.Sin(x * Math.PI / 180)
};
this.SineGraphValues.Add(point);
}
// Setup the data mapper
this.DataMapper = new CartesianMapper<ObservablePoint>()
.X(point => point.X)
.Y(point => point.Y)
.Stroke(point => point.Y > 0.3 ? Brushes.Red : Brushes.LightGreen)
.Fill(point => point.Y > 0.3 ? Brushes.Red : Brushes.LightGreen);
}
private object dataMapper;
public object DataMapper
{
get => this.dataMapper;
set
{
this.dataMapper = value;
OnPropertyChanged();
}
}
public ChartValues<ObservablePoint> SineGraphValues { get; }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) => this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
https://stackoverflow.com/questions/65027296
复制相似问题