gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:09:19 24 4
gpt4 key购买 nike

我的第一篇文章在这里!编码新手!...

我在页面的框架中有一个 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 页面?有没有一种简单的方法可以通过单击一行来打开页面?

谢谢。

最佳答案

一个更好的方法是定义一个 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>

SelectedPersonPersons 在此页面的代码中定义如下:

 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 单击行打开新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27322575/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com