2017-03-02 74 views
1

我在彈出窗口的DataGrid中顯示搜索結果,一旦選擇了一個項目,彈出窗口關閉,所選項目顯示在頁面上。它在第一次打開和關閉時都可以正常工作,但是當我嘗試重新打開彈出窗口時,我得到一個未處理的異常「OnPropertyChanged(」IsOpen「)中發生了」PresentationFramework.dll中發生的類型'System.InvalidOperationException'的未處理異常「調用xaml打開第二次錯誤的彈出框

查看:

<Button x:Name="btSearch" Command="{Binding SearchNoteCommand}"> 
    <Image Source="images/search.jpg"/> 
</Button> 
<GroupBox x:Name="grpSearchPopup" VerticalAlignment="Top" HorizontalAlignment="Left" Height="51" Width="170" BorderBrush="{x:Null}" BorderThickness="0"> 
    <Popup Name="popSearch" IsOpen="{Binding IsOpen}" Margin="122,89,0,0"> 
     <Grid> 
      <DataGrid Name="dgSearchResults" AutoGenerateColumns="False" ItemsSource="{Binding Path=CurrentNote.SearchResults}" SelectedItem="{Binding SelectedItem}" AlternatingRowBackground="#FFED676E" AlternationCount="2" Background="{x:Null}" > 
       <DataGrid.Columns> 
        <DataGridTextColumn Binding="{Binding NoteID}" Width="25" Header="ID"/> 
        <DataGridTextColumn Binding="{Binding Note}" Width="100" Header="Note"/> 
        <DataGridTextColumn Binding="{Binding Date}" Width="70" Header="Date"/> 
        <DataGridTextColumn Binding="{Binding Time}" Width="60" Header="Time"/> 
       </DataGrid.Columns> 
      </DataGrid> 
     </Grid> 
    </Popup> 
</GroupBox> 

視圖模型:

private bool _isOpen; 
public bool IsOpen 
{ 
    get { return _isOpen; } 
    set 
    { 
     if (_isOpen == value) return; 
     _isOpen = value; 
     OnPropertyChanged("IsOpen"); 
    } 
} 
private NoteModel.SearchResult _selectedItem; 
public NoteModel.SearchResult SelectedItem 
{ 
    get { return _selectedItem; } 
    set 
    { 
     if (value != _selectedItem) 
     { 
      _selectedItem = value; 
      OnPropertyChanged("SelectedItem"); 
      if (_selectedItem != null) 
      { 
       noteData.FetchNote(CurrentNote, _selectedItem.NoteID); 
       OnPropertyChanged("CurrentNote"); 
       //close the popup for search results 
       IsOpen = false; 
      } 
     } 
    } 
} 
private ICommand _searchNote; 
public ICommand SearchNoteCommand 
{ 
    get 
    { 
     if (_searchNote == null) 
     { 
      _searchNote = new RelayCommand(
       p => this.CanSearchNote(), 
       p => this.SearchNote()); 
     } 
     return _searchNote; 
    } 
} 
private bool CanSearchNote() 
{ 
    if (Search != null) 
     return true; 
    return false; 
} 
private void SearchNote() 
{ 
    noteData.SearchNotes(CurrentNote, Search); 
    //open the popup for search 
    IsOpen = true; 
} 

錯誤發生在這裏:

protected virtual void OnPropertyChanged(string propertyName) 
{ 
    this.VerifyPropertyName(propertyName); 

    if (this.PropertyChanged != null) 
    { 
     var e = new PropertyChangedEventArgs(propertyName); 
     this.PropertyChanged(this, e); <----- error 
    } 
} 

NoteData.cs

public bool SearchNotes(NoteModel CurrentNote, string sSearch) 
     { 
      //check the open connection 
      if (ConnectData.OpenConnection() == true) 
      { 
       try 
       { 
        string SQLSearch = "select note_id, date, time, note from note where date like '%" + sSearch + "%' or time like '%" + sSearch + "%' or note like '%" + sSearch + "%' group by note_id, date, time, note"; 

        //get the phone types for lookups 
        SqlCommand cmdSearchNotes = new SqlCommand(SQLSearch, ConnectData.connection); 
        SqlDataReader drSearchResults = cmdSearchNotes.ExecuteReader(); 
        CurrentNote.SearchResults = new List<NoteModel.SearchResult>(); 

        while (drSearchResults.Read()) 
        { 
         CurrentNote.SearchResults.Add(new NoteModel.SearchResult() 
         { 
          NoteID = (int)drSearchResults["note_id"], 
          Date = drSearchResults["date"].ToString(), 
          Time = drSearchResults["time"].ToString(), 
          Note = (string)drSearchResults["note"] 
         }); 
        } 
        drSearchResults.Dispose(); 
        cmdSearchNotes.Dispose(); 

        //close Connection 
        ConnectData.CloseConnection(); 

        return true; 
       } 
       catch (SqlException ex) 
       { 
        return false; 
        throw new ApplicationException("Something went wrong with fetching the note search results: ", ex); 
       } 
      } 
      //connection failed 
      else 
      { 
       return false; 
      } 
     } 

public bool FetchNote(NoteModel CurrentNote, int NoteID) 
{ 
    //check the open connection 
    if (ConnectData.OpenConnection() == true) 
    { 
     try 
     { 
      string SQLSearch = "select note_id, date, time, note from note where note_id = " + NoteID; 

      //get the phone types for lookups 
      SqlCommand cmdFetchNote = new SqlCommand(SQLSearch, ConnectData.connection); 
      SqlDataReader drFetchNote = cmdFetchNote.ExecuteReader(); 

      while (drFetchNote.Read()) 
      { 
       CurrentNote.NoteID = (int)drFetchNote["note_id"]; 
       CurrentNote.Date = drFetchNote["date"].ToString(); 
       CurrentNote.Time = drFetchNote["time"].ToString(); 
       CurrentNote.Note = (string)drFetchNote["note"]; 
       CurrentNote.SearchResults = null; 
      } 
      drFetchNote.Dispose(); 
      cmdFetchNote.Dispose(); 

      //close Connection 
      ConnectData.CloseConnection(); 

      return true; 
     } 
     catch (SqlException ex) 
     { 
      return false; 
      throw new ApplicationException("Something went wrong with fetching the note: ", ex); 
     } 
    } 
    //connection failed 
    else 
    { 
     return false; 
    } 
} 

堆棧跟蹤:

σε System.Windows.Controls.ItemContainerGenerator.Verify() 
    σε System.Windows.Controls.VirtualizingStackPanel.MeasureChild(IItemContainerGenerator& generator, IContainItemStorage& itemStorageProvider, IContainItemStorage& parentItemStorageProvider, Object& parentItem, Boolean& hasUniformOrAverageContainerSizeBeenSet, Double& computedUniformOrAverageContainerSize, Double& computedUniformOrAverageContainerPixelSize, Boolean& computedAreContainersUniformlySized, IList& items, Object& item, IList& children, Int32& childIndex, Boolean& visualOrderChanged, Boolean& isHorizontal, Size& childConstraint, Rect& viewport, VirtualizationCacheLength& cacheSize, VirtualizationCacheLengthUnit& cacheUnit, Boolean& foundFirstItemInViewport, Double& firstItemInViewportOffset, Size& stackPixelSize, Size& stackPixelSizeInViewport, Size& stackPixelSizeInCacheBeforeViewport, Size& stackPixelSizeInCacheAfterViewport, Size& stackLogicalSize, Size& stackLogicalSizeInViewport, Size& stackLogicalSizeInCacheBeforeViewport, Size& stackLogicalSizeInCacheAfterViewport, Boolean& mustDisableVirtualization, Boolean isBeforeFirstItem, Boolean isAfterFirstItem, Boolean isAfterLastItem, Boolean skipActualMeasure, Boolean skipGeneration, Boolean& hasBringIntoViewContainerBeenMeasured, Boolean& hasVirtualizingChildren) 
    σε System.Windows.Controls.VirtualizingStackPanel.MeasureOverrideImpl(Size constraint, Nullable`1& lastPageSafeOffset, List`1& previouslyMeasuredOffsets, Nullable`1& lastPagePixelSize, Boolean remeasure) 
    σε System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(Size constraint) 
    σε System.Windows.Controls.Primitives.DataGridRowsPresenter.MeasureOverride(Size constraint) 
    σε System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
    σε System.Windows.UIElement.Measure(Size availableSize) 
    σε System.Windows.ContextLayoutManager.UpdateLayout() 
    σε System.Windows.UIElement.UpdateLayout() 
    σε System.Windows.Interop.HwndSource.SetLayoutSize() 
    σε System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value) 
    σε System.Windows.Interop.HwndSource.set_RootVisual(Visual value) 
    σε System.Windows.Controls.Primitives.Popup.SetRootVisualToPopupRoot() 
    σε System.Windows.Controls.Primitives.Popup.CreateWindow(Boolean asyncCall) 
    σε System.Windows.Controls.Primitives.Popup.OnIsOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    σε System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e) 
    σε System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e) 
    σε System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args) 
    σε System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType) 
    σε System.Windows.DependencyObject.InvalidateProperty(DependencyProperty dp, Boolean preserveCurrentValue) 
    σε System.Windows.Data.BindingExpressionBase.Invalidate(Boolean isASubPropertyChange) 
    σε System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange) 
    σε System.Windows.Data.BindingExpression.ScheduleTransfer(Boolean isASubPropertyChange) 
    σε MS.Internal.Data.ClrBindingWorker.NewValueAvailable(Boolean dependencySourcesChanged, Boolean initialValue, Boolean isASubPropertyChange) 
    σε MS.Internal.Data.PropertyPathWorker.UpdateSourceValueState(Int32 k, ICollectionView collectionView, Object newValue, Boolean isASubPropertyChange) 
    σε MS.Internal.Data.ClrBindingWorker.OnSourcePropertyChanged(Object o, String propName) 
    σε MS.Internal.Data.PropertyPathWorker.OnPropertyChanged(Object sender, PropertyChangedEventArgs e) 
    σε System.Windows.WeakEventManager.ListenerList`1.DeliverEvent(Object sender, EventArgs e, Type managerType) 
    σε System.ComponentModel.PropertyChangedEventManager.OnPropertyChanged(Object sender, PropertyChangedEventArgs args) 
    σε ObservableObject.OnPropertyChanged(String propertyName) στο C:\Users\treej\Source\Repos\FiloFix\FiloFix\Helper Classes\ObservableObject.cs:γραμμή 24 
    σε FiloFix.ViewModel.NoteViewModel.set_IsOpen(Boolean value) στο C:\Users\treej\Source\Repos\FiloFix\FiloFix\ViewModel\NoteViewModel.cs:γραμμή 113 
    σε FiloFix.ViewModel.NoteViewModel.SearchNote() στο C:\Users\treej\Source\Repos\FiloFix\FiloFix\ViewModel\NoteViewModel.cs:γραμμή 50 
    σε FiloFix.ViewModel.NoteViewModel.<get_SearchNoteCommand>b__8_1(Object p) στο C:\Users\treej\Source\Repos\FiloFix\FiloFix\ViewModel\NoteViewModel.cs:γραμμή 35 
    σε RelayCommand.Execute(Object parameter) στο C:\Users\treej\Source\Repos\FiloFix\FiloFix\Helper Classes\RelayCommand.cs:γραμμή 28 
    σε MS.Internal.Commands.CommandHelpers.CriticalExecuteCommandSource(ICommandSource commandSource, Boolean userInitiated) 
    σε System.Windows.Controls.Primitives.ButtonBase.OnClick() 
    σε System.Windows.Controls.Button.OnClick() 
    σε System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) 
    σε System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e) 
    σε System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) 
    σε System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) 
    σε System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) 
    σε System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) 
    σε System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent) 
    σε System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e) 
    σε System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) 
    σε System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) 
    σε System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) 
    σε System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) 
    σε System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) 
    σε System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args) 
    σε System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted) 
    σε System.Windows.Input.InputManager.ProcessStagingArea() 
    σε System.Windows.Input.InputManager.ProcessInput(InputEventArgs input) 
    σε System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport) 
    σε System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel) 
    σε System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
    σε System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
    σε MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
    σε MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) 
    σε System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) 
    σε System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) 
    σε System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) 
    σε MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) 
    σε MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) 
    σε System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) 
    σε System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) 
    σε System.Windows.Application.RunDispa... 

任何想法?在此先感謝

+0

你是如何觸發SearchNoteCommand的。聽起來像是在錯誤的線程上發送的東西。 – bradgonesurfing

+0

它正從視圖搜索按鈕觸發。 –

+0

當異常停止你的程序是哪個線程?看看__threads__窗口。還要確保所有異常都已打開(Debugs/Windows/Exception Settings),以便捕獲第一個異常。 – bradgonesurfing

回答

1

請嘗試以下

public bool SearchNotes(NoteModel CurrentNote, string sSearch) 
    { 
     //check the open connection 
     if (ConnectData.OpenConnection() == true) 
     { 
      try 
      { 
       string SQLSearch = "select note_id, date, time, note from note where date like '%" + sSearch + "%' or time like '%" + sSearch + "%' or note like '%" + sSearch + "%' group by note_id, date, time, note"; 

       //get the phone types for lookups 
       SqlCommand cmdSearchNotes = new SqlCommand(SQLSearch, ConnectData.connection); 
       SqlDataReader drSearchResults = cmdSearchNotes.ExecuteReader(); 

       // CHANGE IS HERE !!!!!!!!!!!!!!!! 
       var list = new List<NoteModel.SearchResult>(); 

       while (drSearchResults.Read()) 
       { 
        CurrentNote.SearchResults.Add(new NoteModel.SearchResult() 
        { 
         NoteID = (int)drSearchResults["note_id"], 
         Date = drSearchResults["date"].ToString(), 
         Time = drSearchResults["time"].ToString(), 
         Note = (string)drSearchResults["note"] 
        }); 
       } 

       // CHANGE IS HERE !!!!!!!!!!!!!!!! 
       CurrentNote.SearchResults = list; 
       drSearchResults.Dispose(); 
       cmdSearchNotes.Dispose(); 

       //close Connection 
       ConnectData.CloseConnection(); 

       return true; 
      } 
      catch (SqlException ex) 
      { 
       return false; 
       throw new ApplicationException("Something went wrong with fetching the note search results: ", ex); 
      } 
     } 
     //connection failed 
     else 
     { 
      return false; 
     } 
    } 

注意我只指定CurrentNote.SearchResults後的列表已滿。第一次打開彈出窗口時,它綁定到列表。下次單擊彈出框時已經綁定,當您撥打電話時

CurrentNote.SearchResults = ... 

WPF中發生了一些操作。然後你去改變列表的內容,這就是導致錯誤的原因。

+1

你絕對明星:) –

+0

//改變這個太 list.Add(新NoteModel.SearchResult(){ NoteID =(int)的drSearchResults [ 「note_id」], 日期= drSearchResults [ 「日」]。的ToString( ), Time = drSearchResults [「time」]。ToString(), Note =(string)drSearchResults [「note」] }); } –

+0

歡迎您:)這是一個很好的解決難題。但一個提示。從異常中獲取消息並將其粘貼到Google中。這是缺少的信息,提示我尋找什麼。 – bradgonesurfing

相關問題