C# 使用visual studio 2012 C在WPF中使用DataGrid示例#
我的DataGrid包含一些项目。当我在DataGrid中选择项目时,它应该显示在我窗口中的3个文本框中。请帮助我使用.cs代码。我是WPF新手 这是我的XAML代码C# 使用visual studio 2012 C在WPF中使用DataGrid示例#,c#,wpf,visual-studio-2012,datagrid,C#,Wpf,Visual Studio 2012,Datagrid,我的DataGrid包含一些项目。当我在DataGrid中选择项目时,它应该显示在我窗口中的3个文本框中。请帮助我使用.cs代码。我是WPF新手 这是我的XAML代码 <Window x:Class="simpledatagrid.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2
<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 Name="dgsample" BorderBrush="Black" BorderThickness="2" AutoGenerateColumns="True" CanUserAddRows="True" CanUserDeleteRows="True" Margin="200,10,10,75"/>
<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
{
ObservableCollection<User> Users = new ObservableCollection<User>();
public MainWindow()
{
InitializeComponent();
Users.Add(new User() { Id = 101, Name = "Allen", Salary = 10 });
Users.Add(new User() { Id = 102, Name = "king", Salary = 20 });
Users.Add(new User() { Id = 103, Name = "scot", Salary = 30 });
Users.Add(new User() { Id = 104, Name = "havy", Salary = 40 });
Users.Add(new User() { Id = 105, Name = "xen", Salary = 50 });
Users.Add(new User() { Id = 106, Name = "len", Salary = 60 });
dgsample.ItemsSource = Users;
}
private void Get_Click(object sender, RoutedEventArgs e)
{
{
User currentUser = Users.Single(select => select.Id == int.Parse(this.tb1.Text));
this.tb2.Text = currentUser.Name;
this.tb3.Text = currentUser.Salary.ToString();
}
}
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);
}
}
公共部分类主窗口:窗口
{
ObservableCollection用户=新的ObservableCollection();
公共主窗口()
{
初始化组件();
添加(新用户(){Id=101,Name=“Allen”,Salary=10});
添加(新用户(){Id=102,Name=“king”,Salary=20});
添加(新用户(){Id=103,Name=“scot”,Salary=30});
添加(新用户(){Id=104,Name=“havy”,Salary=40});
添加(新用户(){Id=105,Name=“xen”,Salary=50});
添加(新用户(){Id=106,Name=“len”,Salary=60});
dgsample.ItemsSource=用户;
}
私有void Get_Click(对象发送方,路由目标)
{
{
User currentUser=Users.Single(select=>select.Id==int.Parse(this.tb1.Text));
this.tb2.Text=currentUser.Name;
this.tb3.Text=currentUser.Salary.ToString();
}
}
私有无效添加\单击(对象发送者,路由目标)
{
Add(newuser(){Id=int.Parse(tb1.Text),Name=tb2.Text,Salary=int.Parse(tb3.Text)});
}
私有无效删除\单击(对象发送者,路由目标)
{
Users.removet(dgsample.SelectedIndex);
}
}
您可以进行以下绑定:
<TextBox Text="{Binding SelectedItem.Name, ElementName=dgsample}"/>
和eny属性的模拟:Id、薪水。您可以进行以下绑定:
<TextBox Text="{Binding SelectedItem.Name, ElementName=dgsample}"/>
和eny属性的模拟:Id、薪水。初始化组件后,在构造函数中将SelectionChanged事件添加到datagrid中:
dgsample.SelectionChanged += Grid_SelectionChanged;
并添加以下代码:
private void Grid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int index = dgsample.SelectedIndex;
tb1.Text = users[index].Id.ToString();
tb2.Text = users[index].Name;
tb3.Text = users[index].Salary.ToString();
}
*更新*我知道这与问题无关,但作者向我要的
以下是您的添加方法:
private void BtnAdd_Click(object sender, RoutedEventArgs e)
{
if (!tb1.Text.Equals("")) //checks if tb1 is not empy
{
var query1 = users.Where(User => User.Id == int.Parse(tb1.Text)); //creates a variable query1 with all users who have same ID as the one in tb1, I do this to block the same ID insertion
if (!query1.Any()) //if the query1 is empty, meaning there are no users with given id
{
users.Add(new User() { Id = int.Parse(tb1.Text), Name = tb2.Text, Salary = int.Parse(tb3.Text) }); // adds new user to the list
}
}
}
初始化组件后,将SelectionChanged事件添加到构造函数中的datagrid中:
dgsample.SelectionChanged += Grid_SelectionChanged;
并添加以下代码:
private void Grid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int index = dgsample.SelectedIndex;
tb1.Text = users[index].Id.ToString();
tb2.Text = users[index].Name;
tb3.Text = users[index].Salary.ToString();
}
*更新*我知道这与问题无关,但作者向我要的
以下是您的添加方法:
private void BtnAdd_Click(object sender, RoutedEventArgs e)
{
if (!tb1.Text.Equals("")) //checks if tb1 is not empy
{
var query1 = users.Where(User => User.Id == int.Parse(tb1.Text)); //creates a variable query1 with all users who have same ID as the one in tb1, I do this to block the same ID insertion
if (!query1.Any()) //if the query1 is empty, meaning there are no users with given id
{
users.Add(new User() { Id = int.Parse(tb1.Text), Name = tb2.Text, Salary = int.Parse(tb3.Text) }); // adds new user to the list
}
}
}
更容易做到这一点
<TextBox **Text="{Binding SelectedItem.Id, ElementName=dgsample}"** Name="tb1" HorizontalAlignment="Left" Height="20" Margin="60,10,0,0" TextWrapping="NoWrap" VerticalAlignment="Top" Width="100" />
<TextBox **Text="{Binding SelectedItem.Name, ElementName=dgsample}"** Name="tb2" HorizontalAlignment="Left" Height="20" Margin="60,60,0,0" TextWrapping="NoWrap" VerticalAlignment="Top" Width="100"/>
<TextBox **Text="{Binding SelectedItem.Salary, ElementName=dgsample}"** Name="tb3" HorizontalAlignment="Left" Height="20" Margin="60,110,0,0" TextWrapping="NoWrap" VerticalAlignment="Top" Width="100"/>
更简单的方法
<TextBox **Text="{Binding SelectedItem.Id, ElementName=dgsample}"** Name="tb1" HorizontalAlignment="Left" Height="20" Margin="60,10,0,0" TextWrapping="NoWrap" VerticalAlignment="Top" Width="100" />
<TextBox **Text="{Binding SelectedItem.Name, ElementName=dgsample}"** Name="tb2" HorizontalAlignment="Left" Height="20" Margin="60,60,0,0" TextWrapping="NoWrap" VerticalAlignment="Top" Width="100"/>
<TextBox **Text="{Binding SelectedItem.Salary, ElementName=dgsample}"** Name="tb3" HorizontalAlignment="Left" Height="20" Margin="60,110,0,0" TextWrapping="NoWrap" VerticalAlignment="Top" Width="100"/>
我认为最好使用bindings@user2889489当然,我已经有了答案,但无法将其放在评论中:/@2889489在我的答案中添加了评论。我认为更好地使用它bindings@user2889489当然,我已经为您提供了答案,但无法将其放在评论中:/@2889489在我的答案中添加了评论。