WPF 柱状图显示数据

<Window x:Class="Wpf180706.Window9"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window9" Height="300" Width="300">
    <Grid>
        <ListBox Name="listBox">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="5">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition></ColumnDefinition>
                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Rectangle Fill="Gray" Width="{Binding Price}"></Rectangle>
                        <TextBlock Text="{Binding Year}"></TextBlock>
                        <TextBlock Text="{Binding Price}" Grid.Column="1"></TextBlock>
                    </Grid>
                    
                </DataTemplate>

            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

</Window>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


namespace Wpf180706
{
    /// <summary>
    /// Interaction logic for Window9.xaml
    /// </summary>
    public partial class Window9 : Window
    {
        public Window9()
        {
            InitializeComponent();
            listBox.ItemsSource = lst;
        }


        public List<MyClass> lst = new List<MyClass>()
        {
            new MyClass(){ Price=120, Year=1990},
            new MyClass(){ Price=50, Year=1950},
            new MyClass(){ Price=220, Year=1998},
            new MyClass(){ Price=150, Year=1992},
            new MyClass(){ Price=100, Year=1991},
            new MyClass(){ Price=190, Year=1999}
        };
        




    }


    public class MyClass
    {
        public int Price { get; set; }
        public int Year { get; set; }


    }
}

原文地址:https://www.cnblogs.com/dxmfans/p/9434615.html