如何在C#中将列表与WPF数据网格绑定?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在C#中将列表与WPF数据网格绑定?相关的知识,希望对你有一定的参考价值。

我似乎无法弄清楚如何将数据从wfp导入列表并绑定到datagrid。

xaml表单显示正常

我创建了属性,我创建了一个继承基类的类,即code360。我在线尝试了很多资源,但没有为我工作。我试图硬编码输入,看看它是否会填充网格,但没办法。像这样

{firstName = "Tim", lastName = "Joy", 
 email = "tim@joy.com", 
 phoneNumber = "0988390243", 
 amount = 200000 }

这是mainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Windows;

namespace Code360
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public class code360
        {
            public string firstName { get; set; }
            public string lastName { get; set; }
            public string email { get; set; }
            public string phoneNumber { get; set; }
            public decimal amount { get; set; }
        }

        public class Code360Manager : List<code360>
        {
            public Code360Manager()
            {
                Add(new code360() {firstName = "Tim", lastName = "Joy", email = "tim@joy.com", phoneNumber = "0988390243", amount = 200000 });
            }
        }

        private void SubmitButton_Click(object sender, RoutedEventArgs e)
        {
            //studentGrid.ItemsSource = Code360Manager();
            MessageBox.Show("This is to test the firstname", firstName.Text);
        }
    }
}

这是mainWindow.xaml

<Window x:Class="Code360.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Code360"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    <Grid>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="20" />
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="20" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="20" />
                <ColumnDefinition Width="auto" />
                <ColumnDefinition Width="auto" />
                <ColumnDefinition Width="auto" />
                <ColumnDefinition Width="auto" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="20" />
            </Grid.ColumnDefinitions>

            <TextBlock Grid.Row="1"  Grid.Column="1" FontSize="36"
                   Text="Code360 Student Record" Grid.ColumnSpan="3"
                   Margin="0,0,0,10" />

            <TextBlock Grid.Column="1" Grid.Row="2"
                   Text="First Name" Margin="0,0,0,10"/>

            <TextBox x:Name="firstName" Grid.Column="2" Grid.Row="2" 
                 Width="150" Margin="0,0,0,10"/>

            <TextBlock Grid.Column="3" Grid.Row="2" Text="Last Name" Margin="0,0,0,10"/>

            <TextBox x:Name="lastName" Grid.Column="4" Grid.Row="2" Width="150" Margin="0,0,0,10"/>

            <TextBlock Grid.Column="1" Grid.Row="3"
                   Text="Email" Margin="0,0,0,10"/>

            <TextBox x:Name="email" Grid.Column="2" Grid.Row="3" 
                 Width="150" Margin="0,0,0,10"/>

            <TextBlock Grid.Column="3" Grid.Row="3"
                   Text="Phone Number" Margin="0,0,0,10"/>

            <TextBox x:Name="phonenumber" Grid.Column="4" Grid.Row="3" 
                 Width="150" Margin="0,0,0,10"/>

            <TextBlock Grid.Column="1" Grid.Row="4"
                   Text="Amount" Margin="0,0,0,10"/>

            <TextBox x:Name="amount" Grid.Column="2" Grid.Row="4" 
                 Width="150" Margin="0,0,0,10"/>


            <Button x:Name="submitButton" Content="Submit" 
                Grid.Column="4" Grid.Row="4" 
                Margin="0,0,0,10" Click="SubmitButton_Click" />

            <DataGrid x:Name="studentGrid"  Grid.Column="1"  Grid.Row="5" MinWidth="20" Grid.ColumnSpan="5" Margin="22,0,45,154" Grid.RowSpan="2" ItemsSource="{StaticResource Code360Manager}">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="First Name" Binding="{Binding firstName}" />
                    <DataGridTextColumn Header="Last Name" Binding="{Binding lastName}" />
                    <DataGridTextColumn Header="Email" 
                                        Binding="{Binding email}" />
                    <DataGridTextColumn Header="Phone Number" Binding="{Binding phonenumber}" />
                    <DataGridTextColumn Header="Amount" Binding="{Binding amount}" />
                </DataGrid.Columns>
                <DataGrid.RowDetailsTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Details}" />
                    </DataTemplate>
                </DataGrid.RowDetailsTemplate>
            </DataGrid>



        </Grid>

    </Grid>
</Window>
答案

1.要使用Binding你需要设置DataContext属性

InitializeComponent();
this.DataContext = this;

2.u需要一个集合来绑定xaml.cs中的DataGrid List<code360> MyList { get; set; } //确保你有这个列表中的数据

3.ItemsSource道具将采取集合所以你需要做它与收集绑定ItemsSource="{Binding MyList } // note the list name is same as the in 2 ponit

这也不是跟随mvvm

另一答案

我在Visual Studio中粘贴了该代码,除此之外,我遇到了这行ItemsSource="{StaticResource Code360Manager}"的问题

从你分享的XAML中我看不到你在哪里定义资源所以请考虑以下内容。

<Grid.Resources>
    <local:Code360Manager x:Key="Code360Manager" />
</Grid.Resources>

在定义网格之后,您将看到数据网格渲染

另一答案

我相信你需要设置DataContext。在InitializeComponent();之后:DataContext = this;编辑:(抱歉。我发了很多评论)。您需要将xaml设置为上下文。通常使用MainWindowViewModel。我更改您的代码以将ItemsSource设置为List <>而不是类:

public partial class MainWindow : Window
{
    public List<code360> Code360Manager { get; set; } = new List<code360>();

    public MainWindow()
    {
        InitializeComponent();

        Code360Manager.Add(new code360() {firstName = "Tim", lastName = "Joy", email = "tim@joy.com", phoneNumber = "0988390243", amount = 200000 });

        DataContext = this;
    }

    private void SubmitButton_Click(object sender, RoutedEventArgs e)
    {
        //studentGrid.ItemsSource = Code360Manager();
        MessageBox.Show("This is to test the firstname", firstName.Text);
    }
}

public class code360
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string email { get; set; }
    public string phoneNumber { get; set; }
    public decimal amount { get; set; }
}
另一答案

Grid不会将数据输入作为ItemSource,您需要使用DataContext。就像是:

studentGrid.DataContext = Code360Manager();

如果仍然无效,请尝试使用ListCollectionView,如下所示:

ListCollectionView alist = new ListCollectionView(Code360Manager());
studentGrid.DataContext = alist;

以上是关于如何在C#中将列表与WPF数据网格绑定?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 wpf 中将新行添加到数据网格中?

在 WPF 中,如何将数据网格列绑定到数据表的特定列?

使用列表将数据网格绑定到两个不同的类?

WPF 数据网格文本列中的绑定

WPF 网格与孩子。如何将子 IsEnabled 绑定到父行

WPF动态数据网格绑定方法