热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C#编程指南:实现列表与WPF数据网格的高效绑定方法

篇首语:本文由编程笔记#小编为大家整理,主要介绍了如何在C#中将列表与WPF数据网格绑定?相关的知识,希望对你有一定的参考价值。 我似乎无法弄清楚如何将数据从wfp导入列表并绑定到datagrid。x

篇首语:本文由编程笔记#小编为大家整理,主要介绍了如何在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
{
///


/// Interaction logic for MainWindow.xaml
///

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
{
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

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" >





















Text="Code360 Student Record" Grid.ColumnSpan="3"
Margin="0,0,0,10" />
Text="First Name" Margin="0,0,0,10"/>
Margin="0,0,0,10"/>


Text="Email" Margin="0,0,0,10"/>
Margin="0,0,0,10"/>
Text="Phone Number" Margin="0,0,0,10"/>
Margin="0,0,0,10"/>
Text="Amount" Margin="0,0,0,10"/>
Margin="0,0,0,10"/>

答案

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

InitializeComponent();
this.DataCOntext= this;

2.u需要一个集合来绑定xaml.cs中的DataGrid List 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中我看不到你在哪里定义资源所以请考虑以下内容。




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


另一答案

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

public partial class MainWindow : Window
{
public List Code360Manager { get; set; } = new List();
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;


推荐阅读
author-avatar
孙衍龙
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有