C# WPF ItemsControl 循环 LiveCharts X点 Y点 绑定Binding
在WPF中使用ItemsControl来循环绑定数据,并使用LiveCharts来绑定X和Y的点。
首先,你需要在你的XAML文件中创建一个ItemsControl来显示数据:
<ItemsControl ItemsSource="{Binding DataPoints}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding X}" />
<TextBlock Text="{Binding Y}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
然后,在你的ViewModel中创建一个名为DataPoints的可观察集合,用于存储X和Y的点:
public class ViewModel : INotifyPropertyChanged
{
private ObservableCollection<DataPoint> _dataPoints;
public ObservableCollection<DataPoint> DataPoints
{
get { return _dataPoints; }
set
{
_dataPoints = value;
OnPropertyChanged(nameof(DataPoints));
}
}
public ViewModel()
{
DataPoints = new ObservableCollection<DataPoint>();
// 添加一些点
DataPoints.Add(new DataPoint(1, 2));
DataPoints.Add(new DataPoint(3, 4));
DataPoints.Add(new DataPoint(5, 6));
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class DataPoint
{
public double X { get; set; }
public double Y { get; set; }
public DataPoint(double x, double y)
{
X = x;
Y = y;
}
}
最后,在你的MainWindow中设置DataContext为ViewModel的实例:
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
这样,你就可以在ItemsControl中循环绑定DataPoints中的点,并使用LiveCharts来显示X和Y的值
原文地址: http://www.cveoy.top/t/topic/hCYy 著作权归作者所有。请勿转载和采集!