ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c# – WPF Datagrid单击行以打开新页面

2019-07-03 02:02:15  阅读:438  来源: 互联网

标签:c click wpf xaml datagrid


我的第一篇帖子!新编码!…

我在页面上的框架中有一个WPF数据网格.我想在一行上单击(最好是单击)并使用存储在其中一列中的ID值导航到(打开)一个新页面.

使用MouseDoubleClick有时我可以双击行来打开一个页面.但有时它会抛出:
“program.exe中发生了’System.NullReferenceException’类型的未处理异常
附加信息:对象引用未设置为对象的实例.“

在线(请参阅下面的代码以获取完整方法):

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>

代码背后:

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页面?有没有一种简单的方法可以单击一行打开页面?

谢谢.

解决方法:

更好的方法是定义一个定义DataGrid ItemSource的Person集合和一个包含DataGrid中所选Item的Person类型的Property:

 <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用于通知UI属性中的任何更改.
当您在DataGrid中收到MouseRightButtonUp事件时,您可以使用SelectedPerson属性或将其传递给新页面:

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

并且您可以更改PersonProfile页面以在其构造函数中接收Person.

标签:c,click,wpf,xaml,datagrid
来源: https://codeday.me/bug/20190703/1362268.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有