首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在LineSeries中使用LiveCharts绑定值属性

如何在LineSeries中使用LiveCharts绑定值属性
EN

Stack Overflow用户
提问于 2020-11-26 18:11:43
回答 1查看 1.4K关注 0票数 0

我希望将WPF中每个行系列的值绑定到C#中类型为C#的值的集合。目前,每一行都指定了实数。我如何将它绑定到一个集合?一个例子是将第一行系列绑定到下面的系列集合。非常感谢!

代码语言:javascript
运行
复制
<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#中:

代码语言:javascript
运行
复制
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);
EN

Stack Overflow用户

回答已采纳

发布于 2020-11-26 19:10:55

当您在XAML中定义LineSeries时,就不必再定义SeriesCollection了(您只是在XAML中这样做-- <CartesianChart.Series>就是SeriesCollection)。在ChartValues中定义实际的C#源代码集合就足够了(例如,在视图模型中):

MainWindow.xaml

代码语言:javascript
运行
复制
<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

代码语言:javascript
运行
复制
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));
}
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65027296

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档