我的窗口中有3个TexBox
es,Button
和DataGrid
。当我将数据输入TextBox
并单击按钮时,它应该添加到DataGrid
。我需要一个代码来添加,删除和获取数据。我是wpf的新手,请帮我解释一下代码。
这是我的Xaml代码
<Window x:Class="simpledatagrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="IDDATA" Height="350" Width="525">
<Grid >
<DataGrid BorderBrush="Black" BorderThickness="2" AutoGenerateColumns="True" Name="dgsample" Margin="200,10,10,75"></DataGrid>
<Label Content="ID :" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="26" Width="27"/>
<Label Content="Name :" HorizontalAlignment="Left" Margin="10,60,0,0" VerticalAlignment="Top" Height="26" Width="48"/>
<Label Content="Salary :" HorizontalAlignment="Left" Margin="10,110,0,0" VerticalAlignment="Top" Height="26" Width="47"/>
<TextBox Name="tb1" HorizontalAlignment="Left" Height="20" Margin="60,10,0,0" TextWrapping="NoWrap" Text="" VerticalAlignment="Top" Width="100"/>
<TextBox Name="tb2" HorizontalAlignment="Left" Height="20" Margin="60,60,0,0" TextWrapping="NoWrap" Text="" VerticalAlignment="Top" Width="100"/>
<TextBox Name="tb3" HorizontalAlignment="Left" Height="20" Margin="60,110,0,0" TextWrapping="NoWrap" Text="" VerticalAlignment="Top" Width="100"/>
<Button Content="Get" HorizontalAlignment="Left" Margin="10,190,0,0" VerticalAlignment="Top" Width="75" Click="Get_Click" />
<Button Content="Add" HorizontalAlignment="Left" Margin="10,230,0,0" VerticalAlignment="Top" Width="75" Click="Add_Click" />
<Button Content="Delete" HorizontalAlignment="Left" Margin="10,270,0,0" VerticalAlignment="Top" Width="75" Click="Delete_Click" />
</Grid>
这是我的.cs代码
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<User> users = new List<User>();
users.Add(new User() { Id = 101, Name = "gin", Salary = 10 });
users.Add(new User() { Id = 102, Name = "alen", Salary = 20 });
users.Add(new User() { Id = 103, Name = "scott", Salary = 30 });
dgsample.ItemsSource = users;
}
private void Get_Click(object sender, RoutedEventArgs e)
{
}
private void Add_Click(object sender, RoutedEventArgs e)
{
}
private void Delete_Click(object sender, RoutedEventArgs e)
{
}
}
}
答案 0 :(得分:1)
首先,您可以使用ObservableCollection而不是List。因为ObservableCollection默认具有INotifyCollectionChanged。 ObservableCollection是TwoWayBinding。
ObservableCollection<User> users = new ObservableCollection<User>();
public MainWindow()
{
InitializeComponent();
users.Add(new User() { Id = 101, Name = "gin", Salary = 10 });
users.Add(new User() { Id = 102, Name = "alen", Salary = 20 });
users.Add(new User() { Id = 103, Name = "scott", Salary = 30 });
dgsample.ItemsSource = users;
}
private void Get_Click(object sender, RoutedEventArgs e)
{
if (this.tb1.Text != string.Empty) { User currentUser = users.Single(select => select.Id == Int32.Parse(this.tb1.Text)); this.tb2.Text = currentUser.Name; this.tb3.Text = currentUser.Salary; }
}
private void Add_Click(object sender, RoutedEventArgs e)
{
users.Add(new User() { Id = 105, Name = "gin5", Salary = 100 });
}
private void Delete_Click(object sender, RoutedEventArgs e)
{
users.RemoveAt(0);
}
答案 1 :(得分:0)
定义用户类:
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public User()
{
}
public User(int id, string name, int salary)
{
Id = id;
Name = name;
Salary = salary;
}
}
然后在您的MainWindow.xaml.cs文件中将List更改为ObservableCollection(您需要添加;
using System.Collections.ObjectModel;
//编辑
然后:
ObservableCollection<User> users;
public MainWindow()
{
users = new ObservableCollection<User>();
users.Add(new User() { Id = 101, Name = "gin", Salary = 10 });
users.Add(new User() { Id = 102, Name = "alen", Salary = 20 });
users.Add(new User() { Id = 103, Name = "scott", Salary = 30 });
InitializeComponent();
dgsample.ItemsSource = users;
}
private void Add_Click(object sender, RoutedEventArgs e)
{
users.Add(new User(){Id = Int.Parse(tb1.Text), Name = tb2.Text, Salary = Int.Parse(tb3.Text)});
}
private void Delete_Click(object sender, RoutedEventArgs e)
{
users.RemoveAt(dgSample.SelectedIndex);
}
// you can get what you need just selected proper User proprety
private void Get_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(( (User)dgSample.Items[dgSample.SelectedIndex]).Name);
}
答案 2 :(得分:0)
定义ObservableCollection
属性设置为ItemsSource
DataGrid
,并在您想要添加/删除DataGrid
中的任何项目时对其进行更新。更新Collection将更新datagrid
public MainWindow()
{
InitializeComponent();
Users = new ObservableCollection<User>;
Users.Add(new User() {Id = 101, Name = "gin", Salary = 10});
Users.Add(new User() {Id = 102, Name = "alen", Salary = 20});
Users.Add(new User() {Id = 103, Name = "scott", Salary = 30});
dgsample.ItemsSource = Users;
}
private ObservableCollection<User> Users { get; set; }
private void Add_Click(object sender, RoutedEventArgs e)
{
Users.Add(new User() {Id = int.Parse(tb1.Text), Name = tb2.Text, Salary = tb3.Text});
}
此外,作为建议,添加/删除功能在DataGrid
中建立,将CanUserAddRows
和CanUserDeleteRows
设置为true允许用户将项目直接添加到DataGrid
,所以你可能不需要你的文本框和按钮来添加/删除
<DataGrid AutoGenerateColumns="True" CanUserAddRows="True" CanUserDeleteRows="True"></DataGrid>
答案 3 :(得分:0)
如果您是WPF的新手,您应该从事WPF way开始。在WPF中,您应该操纵绑定到UI的数据,而不是直接操作UI。此设计模式称为MVVM。
在您的情况下,您将拥有一个类(称为 ViewModel ),它将包含您的User
项的集合(称为模型 )。
public class MainWindowViewModel
{
public MainWindowViewModel()
{
Users = new ObservableCollection<User>();
users.Add(new User() { Id = 101, Name = "gin", Salary = 10 });
users.Add(new User() { Id = 102, Name = "alen", Salary = 20 });
users.Add(new User() { Id = 103, Name = "scott", Salary = 30 });
}
public ObservableCollection<User> Users { get; set; }
}
在您的MainWindow
(称为查看)代码隐藏中,您应该将DataContext
设置为MainWindowViewModel
。
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowViewModel();
}
在窗口本身中,将集合绑定到 ViewModel 中的Users
属性
<DataGrid BorderBrush="Black" BorderThickness="2" AutoGenerateColumns="True" Name="dgsample" Margin="200,10,10,75" ItemsSource="{Binding Users}"></DataGrid>
至于您的TextBox
es,因为它们旨在为新的User
创建一个新的{em> ViewModel
User
属性>
public User NewUser { get; set; }
并将它们绑定到User
类
<TextBox Name="tb1" HorizontalAlignment="Left" Height="20" Margin="60,10,0,0" TextWrapping="NoWrap" Text="" VerticalAlignment="Top" Width="100" Text="{Binding Path=NewUser.Id}"/>
<TextBox Name="tb2" HorizontalAlignment="Left" Height="20" Margin="60,60,0,0" TextWrapping="NoWrap" Text="" VerticalAlignment="Top" Width="100" Text="{Binding Path=NewUser.Name}"/>
<TextBox Name="tb3" HorizontalAlignment="Left" Height="20" Margin="60,110,0,0" TextWrapping="NoWrap" Text="" VerticalAlignment="Top" Width="100" Text="{Binding Path=NewUser.Salary}"/>
添加新User
后,清除它的属性。
对于您的按钮,他们应该通过Commands与 ViewModel 进行通信,然后只使用简单的Add
和Remove
方法操纵集合本身它实现了。您可以详细了解here
希望这有帮助