在WPF中的DataGrid中获取选定的项 [英] Getting selected Item in DataGrid in WPF

查看:71
本文介绍了在WPF中的DataGrid中获取选定的项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据网格和文本框.当我在DataGrid中选择一行时,我希望文本框被该datagrid列的相关数据填充.这是图片.

I have a datagrid and textboxes. When I select a row in DataGrid I want the textboxes to be filled with related data of that datagrid column. Here is the picture.,

我已经在MainWindow.xaml.cs中编写了此代码:

I have written this code in MainWindow.xaml.cs :

private void personelEntityDataGrid_Loaded(object sender, RoutedEventArgs e)
{
    PersonelEntity pers = (PersonelEntity)personelEntityDataGrid.SelectedItem;

    NameBox.Text = pers.Name; // I get exception here
    AgeBox.Text = pers.Age.ToString();
    PhoneNumberBox.Text = pers.PhoneNumber;
    AddresBox.Text = pers.Address;


}

运行代码时,我在编写的行中得到了null引用异常.例外:

When I run the code I get the null reference exception in the line that I wrote. Here is the exception:

{对象引用未设置为对象的实例."}

{"Object reference not set to an instance of an object."}

我猜对象pers为空.你能告诉我如何进行这项工作吗?

I guess the object pers is null. Can you tell me how to make this work?

这是我的xaml文件:

Here is my xaml file :

<Window
        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:Personel" mc:Ignorable="d" x:Class="Personel.MainWindow"
        Title="MainWindow" Height="500" Width="725" Loaded="Window_Loaded">
    <Window.Resources>
        <CollectionViewSource x:Key="personelEntityViewSource" d:DesignSource="{d:DesignInstance {x:Type local:PersonelEntity}, CreateList=True}"/>
    </Window.Resources>
    <Grid DataContext="{StaticResource personelEntityViewSource}" >
        <Label Content="Personnel
               " HorizontalAlignment="Left" Margin="55,47,0,0" VerticalAlignment="Top"/>
        <Button Content="Delete" HorizontalAlignment="Left" Margin="344,47,0,0" VerticalAlignment="Top" Width="75" Click="Delete_Button_Click"/>
        <Button Content="Add" HorizontalAlignment="Left" Margin="477,47,0,0" VerticalAlignment="Top" Width="75" Click="Add_Button_Click"/>
        <Button Content="Update" HorizontalAlignment="Left" Margin="624,47,0,0" VerticalAlignment="Top" Width="75" Click="Update_Button_Click"/>
        <Label Content="Name" HorizontalAlignment="Left" Margin="55,91,0,0" VerticalAlignment="Top"/>
        <Label Content="Age" HorizontalAlignment="Left" Margin="55,132,0,0" VerticalAlignment="Top"/>
        <Label Content="Phone Number" HorizontalAlignment="Left" Margin="55,178,0,0" VerticalAlignment="Top"/>
        <Label Content="Address" HorizontalAlignment="Left" Margin="55,218,0,0" VerticalAlignment="Top"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="309,95,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="367" Name="NameBox"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="309,136,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="57" Name="AgeBox"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="309,178,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" Name="PhoneNumberBox"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="309,222,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="367" Name="AddresBox"/>
        <DataGrid x:Name="personelEntityDataGrid" AutoGenerateColumns="False" SelectionChanged="personelEntityDataGrid_SelectionChanged"  EnableRowVirtualization="True" ItemsSource="{Binding Path=.}" Margin="10,291,0,-22" RowDetailsVisibilityMode="VisibleWhenSelected" Loaded="personelEntityDataGrid_Loaded">
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="addressColumn" Binding="{Binding Address}" Header="Address" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="ageColumn" Binding="{Binding Age}" Header="Age" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="idColumn" Binding="{Binding Id}" Header="Id" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="nameColumn" Binding="{Binding Name}" Header="Name" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="phoneNumberColumn" Binding="{Binding PhoneNumber}" Header="Phone Number" Width="SizeToHeader"/>
            </DataGrid.Columns>
        </DataGrid>

    </Grid>
</Window>

推荐答案

不要签入Loaded事件,而是签入 selectionChanged 事件,还添加 null 检查到您的个人对象

Don't check in Loaded event, instead Check in selectionChanged event, also add a null check to your pers object

private void personelEntityDataGrid_SelectionChanged(object sender, RoutedEventArgs e)
{
    PersonelEntity pers = (PersonelEntity)personelEntityDataGrid.SelectedItem;
    if(pers != null)
    {
    NameBox.Text = pers.Name; // I get exception here
    AgeBox.Text = pers.Age.ToString();
    PhoneNumberBox.Text = pers.PhoneNumber;
    AddresBox.Text = pers.Address;
    }

}

这篇关于在WPF中的DataGrid中获取选定的项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆