在C#的WPF应用程序中,可以使用ItemsControl来循环显示数据,并使用LiveCharts库来绘制图表。以下是一个示例,演示如何将X和Y轴数据绑定到ItemsControl中:

在XAML中,定义一个ItemsControl,并设置其ItemsSource属性为绑定的数据源。在ItemsControl中,使用DataTemplate来定义每个项的外观,包括X和Y轴的绑定。

<ItemsControl ItemsSource="{Binding DataPoints}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding X}" />
                <TextBlock Text="{Binding Y}" />
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

在ViewModel中,创建一个属性来存储数据源,并在构造函数中初始化该属性。数据源应包含具有X和Y属性的对象。

public class ViewModel
{
    public ObservableCollection<DataPoint> DataPoints { get; set; }

    public ViewModel()
    {
        DataPoints = new ObservableCollection<DataPoint>()
        {
            new DataPoint { X = 1, Y = 2 },
            new DataPoint { X = 2, Y = 4 },
            new DataPoint { X = 3, Y = 6 }
        };
    }
}

public class DataPoint
{
    public double X { get; set; }
    public double Y { get; set; }
}

最后,在MainWindow的构造函数中,将ViewModel设置为DataContext。

public MainWindow()
{
    InitializeComponent();
    DataContext = new ViewModel();
}

这样,ItemsControl将循环显示数据源中的每个DataPoint,并将每个DataPoint的X和Y属性绑定到TextBlock中进行显示

C# WPF ItemsControl 循环 LiveCharts X Y 绑定Binding

原文地址: http://www.cveoy.top/t/topic/hCYv 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录