关于WPF DataGrid向表格输入数据,并保存在数组里面。

rengu4269 2018-06-18 06:38:44
我现在想实现向DataGrid里面输入数据,然后存入数组以进行数据处理。界面如下,求大神给出具体代码,有重谢!在网上搜了很多还是无从下手。
...全文
2231 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
酒醉夜 2018-10-11
  • 打赏
  • 举报
回复
老铁,你这个表格控件不像是DataGrid啊,好漂亮呀,怎么弄的啊?谢谢
qq14923349 2018-06-27
  • 打赏
  • 举报
回复
引用 9 楼 rengu4269 的回复:
我只要实现这个功能的代码,我只想只想学习一下,并不作为商业用途
https://www.cnblogs.com/santian/p/4366832.html
qq14923349 2018-06-27
  • 打赏
  • 举报
回复
引用 9 楼 rengu4269 的回复:
我只要实现这个功能的代码,我只想只想学习一下,并不作为商业用途

<Window x:Class="WpfApplication1.WindowObservable"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window8" Height="356" Width="471">
<Grid>
<StackPanel Height="295" HorizontalAlignment="Left" Margin="10,10,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="427">
<TextBlock Height="23" Name="textBlock1" Text="学员编号:" />
<TextBox Height="23" Name="txtStudentId" Width="301" HorizontalAlignment="Left"/>
<TextBlock Height="23" Name="textBlock2" Text="学员列表:" />
<ListBox Height="156" Name="lbStudent" Width="305" HorizontalAlignment="Left">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Name="stackPanel2" Orientation="Horizontal">
<TextBlock Text="{Binding Id,Mode=TwoWay}" Margin="5" Background="Beige"/>
<TextBlock Text="{Binding Name,Mode=TwoWay}" Margin="5"/>
<TextBlock Text="{Binding Age,Mode=TwoWay}" Margin="5"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="Button" Height="23" Name="button1" Width="75" HorizontalAlignment="Left" Click="button1_Click" />
</StackPanel>
</Grid>
</Window>


后台cs

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace WpfApplication1
{
public partial class WindowObservable : Window
{
ObservableCollection<Students> infos = new ObservableCollection<Students>() {
new Students(){ Id=1, Age=11, Name="Tom"},
new Students(){ Id=2, Age=12, Name="Darren"},
new Students(){ Id=3, Age=13, Name="Jacky"},
new Students(){ Id=4, Age=14, Name="Andy"}
};

public WindowObservable()
{
InitializeComponent();

this.lbStudent.ItemsSource = infos;

this.txtStudentId.SetBinding(TextBox.TextProperty, new Binding("SelectedItem.Id") { Source = lbStudent });
}
private void button1_Click(object sender, RoutedEventArgs e)
{
infos[1] = new Students() { Id = 4, Age = 14, Name = "这是一个集合改变" };
infos[2].Name = "这是一个属性改变";
}

public class Students
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
}
}
复制代码
在这个例子中我们将Students数据对象用ObservableCollection<T>来修饰。这样当我们点击click的时候我们看到。当我们点击后只有student整个对象的改变引发了后台通知机制。
rengu4269 2018-06-26
  • 打赏
  • 举报
回复
我只要实现这个功能的代码,我只想只想学习一下,并不作为商业用途
qq14923349 2018-06-25
  • 打赏
  • 举报
回复
引用 7 楼 rengu4269 的回复:
这样吧,能用WPF实现这个功能,然后把代码发到我邮箱1757742403@qq.com,我认为满意后会将报酬打到你的支付宝 上面那个邮箱发错了

你是连业务代码都要吧?
rengu4269 2018-06-24
  • 打赏
  • 举报
回复
这样吧,能用WPF实现这个功能,然后把代码发到我邮箱1757742403@qq.com,我认为满意后会将报酬打到你的支付宝 上面那个邮箱发错了
rengu4269 2018-06-22
  • 打赏
  • 举报
回复
能贴出具体代码吗,我是初学者,谢谢了!有重谢
qq14923349 2018-06-22
  • 打赏
  • 举报
回复
ObservableCollection 绑定 mvvm模式
主要是mvvm模式了解一下
ObservableCollection里面元素要提交一下
ylly11111 2018-06-20
  • 打赏
  • 举报
回复
是的,用ObservableCollection绑定DataGrid,新增时,在ViewModel中添加ObservableCollection新行,DataGrid会自动添加一行的
  • 打赏
  • 举报
回复
尽量不要存放到数组,而是使用ObservableCollection动态数据集合,输入结束之后数据自动更新到集合,操作这个集合即可。
qiuqingpo 2018-06-19
  • 打赏
  • 举报
回复
对,楼上说的有道理,把gridview的数据源绑定成ObservableCollection,gridview数据变化了,ObservableCollection也会自动变化,双向绑定的。保存数据时只用获取ObservableCollection的内容就可以了。

8,756

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧