WPF DataGrid绑定到自定义类

时间:2014-01-21 19:01:24

标签: c# wpf datagrid

我认为解决方案应该非常简单。我想要完成的只是显示我的自定义类型列表中包含的数据。我只是想显示数据,我不需要做任何类型的数据操作,我会对最简单的解决方案感兴趣。第一个网格工作正常,它显示数据,但是,我只是无法填充第二个。你能指出我正确的方向吗?在网上找不到类似的东西。感谢

以下是我的两个数据网格:

    <DataGrid x:Name="grdMyData" HorizontalAlignment="Left" Margin="78,78,0,0" VerticalAlignment="Top" Height="94" Width="321"/>

    <DataGrid x:Name="grdMyData2" ItemsSource="{Binding XPath=Car}" HorizontalAlignment="Left" Height="101" Margin="78,198,0,0" VerticalAlignment="Top" Width="321">       
    </DataGrid>

代码背后:

    private List<Car> myCars = new List<Car>();
    private List<Vehicle> myVehicles = new List<Vehicle>();

    private class Vehicle
    {
        public Car MyCar { get; set; }
    }

    private class Car
    {
        public string Type { get; set; }
        public string Color { get; set; }
        public int DoorsNo { get; set; }
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        var car1 = new Car(){Type = "hatchback", Color = "blue", DoorsNo = 5};
        var car2 = new Car() { Type = "sedan", Color = "red", DoorsNo = 4 };
        var car3 = new Car() { Type = "sedan", Color = "blach", DoorsNo = 4 };

        myCars.Add(car1);
        myCars.Add(car2);
        myCars.Add(car3);

        grdMyData.ItemsSource = myCars;

        var vehicle1 = new Vehicle() {MyCar = car1};
        var vehicle2 = new Vehicle() {MyCar = car2};
        var vehicle3 = new Vehicle() {MyCar = car3};
        myVehicles.Add(vehicle1);
        myVehicles.Add(vehicle2);
        myVehicles.Add(vehicle3);

        grdMyData2.DataContext = myVehicles;
    }

这是检索汽车列表的最简单方法,还是有更好的选择

    private List<Car> GetVehicleCars()
    {
        var vehicleCars = new List<Car>();

        foreach (Vehicle v in myVehicles)
        {
            vehicleCars.Add(v.MyCar);   
        }
        return vehicleCars;
    }



grdMyData2.ItemsSource = GetVehicleCars();

1 个答案:

答案 0 :(得分:0)

我认为这就是你想要的。只需将其复制并粘贴到WPF应用程序中即可。 我稍微更改了代码,我想你将能够理解它。

请注意,这是非常非常丑陋的代码,您永远不应该编写这样的代码。我给出了这个丑陋,令人不快的答案,以保持代码与你所写的相似。

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <DataGrid Grid.Row ="0" x:Name="grdMyData" HorizontalAlignment="Left" VerticalAlignment="Top" />
    <DataGrid Grid.Row ="1" x:Name="grdMyData2" HorizontalAlignment="Left" VerticalAlignment="Top"></DataGrid>
    <Button Grid.Row="2" Click="ButtonBase_OnClick">PRESS ME</Button>
</Grid>

背后的代码:

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        //private List<Car> myCars = new List<Car>();
        private List<Vehicle> myVehicles = new List<Vehicle>();

        private class Vehicle
        {
            public Car MyCar { get; set; }
        }

        private abstract class Car
        {
            public string Type { get; set; }
            public string Color { get; set; }
            public int DoorsNo { get; set; }
        }

        private class SimpleCar : Car { }
        private class FancyCar : Car { }

        public MainWindow()
        {
            InitializeComponent();
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            var car1 = new SimpleCar() { Type = "hatchback", Color = "blue", DoorsNo = 5 };
            var car2 = new SimpleCar() { Type = "sedan", Color = "red", DoorsNo = 4 };
            var car3 = new SimpleCar() { Type = "sedan", Color = "blach", DoorsNo = 4 };

            var car4 = new FancyCar() { Type = "fancy", Color = "white", DoorsNo = 3 };
            var car5 = new FancyCar() { Type = "veryFancy", Color = "yellow", DoorsNo = 3 };

            //myCars.Add(car1);
            //myCars.Add(car2);
            //myCars.Add(car3);

            var vehicle1 = new Vehicle() { MyCar = car1 };
            var vehicle2 = new Vehicle() { MyCar = car2 };
            var vehicle3 = new Vehicle() { MyCar = car3 };

            var vehicle4 = new Vehicle(){MyCar = car4};
            var vehicle5 = new Vehicle(){MyCar = car5};

            myVehicles.Add(vehicle1);
            myVehicles.Add(vehicle2);
            myVehicles.Add(vehicle3);
            myVehicles.Add(vehicle4);
            myVehicles.Add(vehicle5);


            var onlySimpleCars = from vehicle in myVehicles
                                  where (vehicle.MyCar.GetType() == typeof(SimpleCar))
                                  select vehicle.MyCar;

            var allCars = from vehicle in myVehicles
                              select vehicle.MyCar;

            grdMyData.ItemsSource = onlySimpleCars.ToList();
            grdMyData2.ItemsSource = allCars.ToList();
        }
    }
}