WPF Datagrid - single line click to open a new page

My first post is here! And new for coding! ...

I have a Datagrid WPF framed on the page. I would like to click (preferably with one click) a row and use the identifier value stored in one of the columns to go to (open) a new page.

Using MouseDoubleClick, sometimes I can double-click a line to open the page. But sometimes it throws: "An unhandled exception of type" System.NullReferenceException "occurred in the program.exe file. Additional information: The object reference is not installed in the object instance."

in line (see below code for the full method):

string ID = ((DataRowView)PersonDataGrid.SelectedItem).Row["PersonID"].ToString();

XAML:

<DataGrid x:Name="PersonDataGrid" AutoGenerateColumns="False" 
     SelectionMode="Single" SelectionUnit ="FullRow"      
     MouseDoubleClick="PersonDataGrid_CellClicked"  >
  <DataGrid.Columns>
      <DataGridTextColumn Binding="{Binding Path=PersonID}" 
          ClipboardContentBinding="{x:Null}" Header="ID"  />
  </DataGrid.Columns>
  <DataGrid.CellStyle>
      <Style TargetType="DataGridCell" BasedOn="{StaticResource myDataGridCellStyle}">
           <EventSetter Event="DataGridCell.MouseLeftButtonDown" Handler="PersonDataGrid_CellClicked"/>
      </Style>
   </DataGrid.CellStyle>
</DataGrid>

Code for:

private void PersonDataGrid_CellClicked(object sender, MouseButtonEventArgs e)

    {
        string ID = ((DataRowView)PersonDataGrid.SelectedItem).Row["PersonID"].ToString();

        SelectedPersonID = Int32.Parse(ID);

        this.NavigationService.Navigate(new PersonProfile());
    }

PersonProfile? , ?

.

+4
2

- Person, DataGrid ItemSource Person, DataGrid:

 <DataGrid x:Name="PersonDataGrid" AutoGenerateColumns="False" 
            SelectionMode="Single" SelectionUnit ="FullRow"       
            MouseRightButtonUp="PersonDataGrid_CellClicked" 
              ItemsSource="{Binding Persons}" 
              SelectedItem="{Binding SelectedPerson}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=PersonId}" Header="Id"  />
            <DataGridTextColumn Binding="{Binding Path=PersonName}" Header="Name"  />
        </DataGrid.Columns>
    </DataGrid>

SelectedPerson Persons :

 public partial class Page1 : Page,INotifyPropertyChanged
{
    private ObservableCollection<Person> _persons ;
    public ObservableCollection<Person> Persons
    {
        get
        {
            return _persons;
        }

        set
        {
            if (_persons == value)
            {
                return;
            }

            _persons = value;
             OnPropertyChanged();
        }
    }

    private Person _selectedPerson ;
    public Person SelectedPerson
    {
        get
        {
            return _selectedPerson;
        }

        set
        {
            if (_selectedPerson == value)
            {
                return;
            }

            _selectedPerson = value;
            OnPropertyChanged();
        }
    }
    public Page1()
    {
        InitializeComponent();
    }

    public event PropertyChangedEventHandler PropertyChanged;
    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
public class Person
{
   public int PersonId { get; set; }
   public string PersonName { get; set; }
}

INotifyPropertyChanged . SelectedPerson , MouseRightButtonUp, , DataGrid:

    private void PersonDataGrid_CellClicked(object sender, MouseButtonEventArgs e)
    {
        if (SelectedPerson == null)
            return;
        this.NavigationService.Navigate(new PersonProfile(SelectedPerson));
    }

PersonProfile, Person .

+2

, , , SelectedItem null. , , . , MVVM. SelectedItem . , , .

0

All Articles