c# – WPF Datagrid单击行以打开新页面
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – WPF Datagrid单击行以打开新页面,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3184字,纯文字阅读大概需要5分钟。
内容图文
![c# – WPF Datagrid单击行以打开新页面](/upload/InfoBanner/zyjiaocheng/779/a64d5f6209d44f88b50287ad9ba93efd.jpg)
我的第一篇帖子!新编码!…
我在页面上的框架中有一个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# – WPF Datagrid单击行以打开新页面全部内容,希望文章能够帮你解决c# – WPF Datagrid单击行以打开新页面所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。