2017-03-26 3 views
1

다음 데이터베이스가 http://merc.tv/img/fig/Northwind_diagram.jpg이고 직원을 클릭하면 해당 직원이 수행 한 주문을 보여주는 WPF 응용 프로그램을 만들고 있습니다. 이건 내 WPF 코드WPF 데이터베이스 Null 참조 예외

public List<Order> orders { 
    get { return selEmp.Orders.ToList(); } 
} 

입니다 : 내가 코드를 실행할 때마다, 나는이 부분에서 NullReferenceException이 얻을

<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에는 주문이 포함되어야합니다.

enter image description here

업데이트 : 디버그 모드를 사용하여 소프트웨어를 통해 갈 때, 실행하고 모든 주문을 보여줍니다,하지만 난 여전히 null 참조 모든

답변

0

첫째을 얻을 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이 아니어야합니다.

+0

? 내 문제를 해결했다. 고맙습니다! – Michael

+0

언급이 없습니다. 그러나'List'는 바인딩을위한 것이 아닙니다. ObservableCollection을 사용하십시오. 게다가'INotifyPropertyChanged'를 사용하면 구문 정글을 만들 수 있습니다. 그걸 정리하고 싶을거야. –

+0

'(selEmp.Orders == null? null : selEmp.Orders.ToList())'를'selEmp.Orders? .ToList()'로보다 우아하게 작성할 수 있습니다. –