WPF数据库空引用异常
WPF database Null Reference Exception
我有以下数据库 http://merc.tv/img/fig/Northwind_diagram.jpg,我正在制作一个 WPF 应用程序,当我单击某个员工时,它会显示该员工完成的订单。每当我 运行 代码时,我都会在这部分得到 NullReferenceException:
public List<Order> orders {
get { return selEmp.Orders.ToList(); }
}
这是我的 WPF 代码:
<Window x:Class="_ForPLUENorthwind1.MainWindow"
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:_ForPLUENorthwind1"
xmlns:localn="clr-namespace:_ForPLUENorthwind1.model"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ObjectDataProvider x:Key="vm" ObjectType="{x:Type localn:viewmodel}" />
</Window.Resources>
<Grid DataContext="{Binding Source={StaticResource vm}}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<ListBox x:Name="liemp" Grid.Column="0" Grid.Row="0"
ItemsSource="{Binding Allemp}"
DisplayMemberPath="FirstName"
SelectedValuePath="EmployeeID"
SelectedItem="{Binding Path=selEmp, Mode=TwoWay}"
/>
<ListBox x:Name="liorders" Grid.Column="1" Grid.Row="0" ItemsSource="{Binding orders}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<Run Text="{Binding OrderID}" />
<Run Text="{Binding OrderDate}" />
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<StackPanel Grid.Column="2" Grid.Row="0">
<StackPanel>
<TextBlock Text="" />
<TextBox />
<TextBlock Text="" />
<TextBox />
<TextBlock Text="" />
<TextBox />
</StackPanel>
<Button x:Name="edit">Edit</Button>
<Button x:Name="add" >Add</Button>
</StackPanel>
</Grid>
</Window>
这是我的 viewmodel.cs:
class viewmodel : INotifyPropertyChanged
{
NorthwindEntities db = new NorthwindEntities();
public event PropertyChangedEventHandler PropertyChanged;
private Employee _emp;
public List<Employee> Allemp {
get { return db.Employees.ToList(); }
}
public Employee selEmp {
get { return _emp; }
set {
_emp = value;
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs("orders"));
}
}
}
public List<Order> orders {
get { return selEmp.Orders.ToList(); }
}
}
第二个 ListBox 应包含订单
更新:当我使用调试模式浏览软件时,它 运行s 并显示所有订单,但我仍然得到空引用
首先,将此用于 setter
:
set {
_emp = value;
NotifyPropertyChange();
if _emp != null
NotifyPropertyChange("orders");
}
而 NotifyPropertyChange
应该是
private void NotifyPropertyChange([CallerMemberName]string prop = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
}
并将 orders
更改为:
public List<Order> orders {
get { return (selEmp.Orders==null? null : selEmp.Orders.ToList()); }
}
看看你此时是否得到 NullReferenceException
。即使那样,您也可能会遇到绑定问题。您应该使用 ObservableCollection
进行绑定,而不是 List
。
我有以下数据库 http://merc.tv/img/fig/Northwind_diagram.jpg,我正在制作一个 WPF 应用程序,当我单击某个员工时,它会显示该员工完成的订单。每当我 运行 代码时,我都会在这部分得到 NullReferenceException:
public List<Order> orders {
get { return selEmp.Orders.ToList(); }
}
这是我的 WPF 代码:
<Window x:Class="_ForPLUENorthwind1.MainWindow"
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:_ForPLUENorthwind1"
xmlns:localn="clr-namespace:_ForPLUENorthwind1.model"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ObjectDataProvider x:Key="vm" ObjectType="{x:Type localn:viewmodel}" />
</Window.Resources>
<Grid DataContext="{Binding Source={StaticResource vm}}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<ListBox x:Name="liemp" Grid.Column="0" Grid.Row="0"
ItemsSource="{Binding Allemp}"
DisplayMemberPath="FirstName"
SelectedValuePath="EmployeeID"
SelectedItem="{Binding Path=selEmp, Mode=TwoWay}"
/>
<ListBox x:Name="liorders" Grid.Column="1" Grid.Row="0" ItemsSource="{Binding orders}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<Run Text="{Binding OrderID}" />
<Run Text="{Binding OrderDate}" />
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<StackPanel Grid.Column="2" Grid.Row="0">
<StackPanel>
<TextBlock Text="" />
<TextBox />
<TextBlock Text="" />
<TextBox />
<TextBlock Text="" />
<TextBox />
</StackPanel>
<Button x:Name="edit">Edit</Button>
<Button x:Name="add" >Add</Button>
</StackPanel>
</Grid>
</Window>
这是我的 viewmodel.cs:
class viewmodel : INotifyPropertyChanged
{
NorthwindEntities db = new NorthwindEntities();
public event PropertyChangedEventHandler PropertyChanged;
private Employee _emp;
public List<Employee> Allemp {
get { return db.Employees.ToList(); }
}
public Employee selEmp {
get { return _emp; }
set {
_emp = value;
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs("orders"));
}
}
}
public List<Order> orders {
get { return selEmp.Orders.ToList(); }
}
}
第二个 ListBox 应包含订单
更新:当我使用调试模式浏览软件时,它 运行s 并显示所有订单,但我仍然得到空引用
首先,将此用于 setter
:
set {
_emp = value;
NotifyPropertyChange();
if _emp != null
NotifyPropertyChange("orders");
}
而 NotifyPropertyChange
应该是
private void NotifyPropertyChange([CallerMemberName]string prop = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
}
并将 orders
更改为:
public List<Order> orders {
get { return (selEmp.Orders==null? null : selEmp.Orders.ToList()); }
}
看看你此时是否得到 NullReferenceException
。即使那样,您也可能会遇到绑定问题。您应该使用 ObservableCollection
进行绑定,而不是 List
。