2015-10-06 32 views
0

在自定義ListView控件上,我正在開發一個「導出到Excel」小按鈕,將ListView的所有內容發送到Excel。由於此按鈕嵌入在ListView控件內,我不知道列出了哪種對象或顯示了哪些列。如何臨時禁用WPF Listview虛擬化?

所以我的方法是通用的,只是解析所有列標題和所有行以將數據導出到Excel。但是當解析不可見的ListViewItem時,您會得到空結果。一個解決方案是在我的ListView上設置VirtualizingStackPanel.VirtualizationMode =「False」,但是當你有成千上萬行時,它會強烈影響GUI體驗。

我正在尋找一種方法來加載所有ListViewItem只是爲了我的通用導出方法,並保持GUI的虛擬化。

有人可以幫我嗎?

謝謝。

+0

我不明白你想要的,關閉虛擬化的唯一方法是設置'什麼VirtualizingStackPanel .IsVirtualizingProperty'爲false。您可以在完成您的方法後再次打開它。 – Hopeless

+1

您應該只爲您的任務使用底層數據,而不是生成的UI容器。 – metacircle

+0

Uderlying數據是來自SQL數據庫的表數據,即我顯示的列(甚至是鏈表)的更多字段。我的應用程序中有很多ListView,這意味着我將不得不返回到每個視圖並定義應該導出到Excel的內容。所以我正在尋找一種方法來強制ListView通過代碼加載所有項目。 – Karnalta

回答

0

我終於做到了反思來解決我的問題。基本上,我首先解析所有列以找到綁定的屬性名稱。然後對ListView中的Items集合進行一些思考以找到屬性值。

下面是代碼(尚未打磨):

private void OnExportToExcel(object sender, ExecutedRoutedEventArgs args) 
    { 
     if (this.Items.Count > 0) 
     { 
      // Ensure we have a visible and selected item 
      if (this.SelectedItem == null) 
       this.SelectedItem = this.Items[0]; 
      this.ScrollIntoView(this.SelectedItem); 

      // Allow UI thread to update first or our selected lvi will be null if not visible 
      UIHelpers.AllowUIToUpdate(); 

      List<string> columnPropertyBindingPathList = new List<string>(); 
      int colIndex = 0; 

      // Get column binding path of underlying data 
      foreach (GridViewColumn h in ((GridView)this.View).Columns) 
      { 
       string bindingPath = ""; 

       if (h.DisplayMemberBinding != null) 
       { 
        bindingPath = (h.DisplayMemberBinding as Binding).Path.Path; 
       } 
       else 
       { 
        ListViewItem lvi = this.ItemContainerGenerator.ContainerFromIndex(this.SelectedIndex) as ListViewItem; 
        if (lvi != null) 
        { 
         GridViewRowPresenter gvp = UIHelpers.FindVisualChild<GridViewRowPresenter>(lvi); 
         DependencyObject col = VisualTreeHelper.GetChild(gvp, colIndex); 
         TextBlock tb = col as TextBlock; 
         if (tb == null) 
         { 
          tb = UIHelpers.FindVisualChild<TextBlock>(col); 
          bindingPath = BindingOperations.GetBinding(tb, TextBlock.TextProperty).Path.Path; 
         } 
        } 
       } 

       colIndex++; 
       if (bindingPath != "") 
        columnPropertyBindingPathList.Add(bindingPath); 
      } 

      if (columnPropertyBindingPathList.Count > 0) 
      { 
       Mouse.SetCursor(Cursors.Wait); 

       // Init array to export to excel 
       object[,] dataArray = new object[this.Items.Count + 1, columnPropertyBindingPathList.Count]; 

       // Add column header 
       for (int colCnt = 0; colCnt < columnPropertyBindingPathList.Count; colCnt++) 
        dataArray[0, colCnt] = columnPropertyBindingPathList[colCnt]; 

       // Reflection to read underlying objects 
       int rowCnt = 1; 
       foreach (object obj in this.Items) 
       { 
        for (int colCnt = 0; colCnt < columnPropertyBindingPathList.Count; colCnt++) 
         dataArray[rowCnt, colCnt] = UIHelpers.GetPropValue(columnPropertyBindingPathList[colCnt], obj); 

        rowCnt++; 
       } 

       Mouse.SetCursor(Cursors.Arrow); 

       // Throw event 
       RoutedEventArgs newEventArgs = new RoutedEventArgs(EmListView.ExportToExcelEvent); 
       newEventArgs.Source = dataArray; 
       RaiseEvent(newEventArgs); 
      } 
      else 
      { 
       // Throw event 
       RoutedEventArgs newEventArgs = new RoutedEventArgs(EmListView.ExportToExcelEvent); 
       newEventArgs.Source = null; 
       RaiseEvent(newEventArgs); 
      } 
     } 
     else 
     { 
      // Throw event 
      RoutedEventArgs newEventArgs = new RoutedEventArgs(EmListView.ExportToExcelEvent); 
      newEventArgs.Source = null; 
      RaiseEvent(newEventArgs); 
     } 
    } 

UIHelpers方法:

/// <summary> 
    /// Give the UI thread the time to refresh 
    /// </summary> 
    public static void AllowUIToUpdate() 
    { 
     DispatcherFrame frame = new DispatcherFrame(); 

     Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Render, new DispatcherOperationCallback(delegate(object parameter) 
     { 
      frame.Continue = false; 
      return null; 
     }), null); 

     Dispatcher.PushFrame(frame); 
    } 

    /// <summary> 
    /// Find a visual child type inside a DependencyObject 
    /// </summary> 
    /// <typeparam name="T"></typeparam> 
    /// <param name="obj"></param> 
    /// <returns></returns> 
    public static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject 
    { 
     for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) 
     { 
      DependencyObject child = VisualTreeHelper.GetChild(obj, i); 
      if (child != null && child is T) 
       return (T)child; 
      else 
      { 
       T childOfChild = FindVisualChild<T>(child); 
       if (childOfChild != null) 
        return childOfChild; 
      } 
     } 
     return null; 
    } 

    /// <summary> 
    /// Find a property value inside a object (even for multi level property) 
    /// </summary> 
    /// <param name="name"></param> 
    /// <param name="obj"></param> 
    /// <returns></returns> 
    public static Object GetPropValue(String name, Object obj) 
    { 
     foreach (String part in name.Split('.')) 
     { 
      if (obj == null) { return null; } 

      Type type = obj.GetType(); 
      PropertyInfo info = type.GetProperty(part); 

      if (info == null) { return null; } 

      obj = info.GetValue(obj, null); 
     } 

     return obj; 
    }