在WPF中的DataGrid中获取所选项

时间:2014-06-27 11:28:28

标签: c# wpf xaml datagrid

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

enter image description here

我在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;


}

当我运行代码时,我在我写的行中得到了空引用异常。这是一个例外:

  

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

我猜对象pers是null。你能告诉我如何使这项工作吗?

这是我的xaml文件:

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

2 个答案:

答案 0 :(得分:2)

请勿检入已加载的事件,而是检入 selectionChanged 事件,同时向您的pers对象添加 null 检查

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;
    }

}

答案 1 :(得分:1)

以下是如何使用绑定来实现的。 &#34;文字&#34;应该修改每个文本框的参数:

Text = "{Binding ElementName = personelEntityDataGrid, Path = CurrentItem.<FIELD_NAME_HERE>}"

因此对于nameBox,您将拥有:

Text = "{Binding ElementName = personelEntityDataGrid, Path = CurrentItem.Name}"