我終於做到了反思來解決我的問題。基本上,我首先解析所有列以找到綁定的屬性名稱。然後對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;
}
我不明白你想要的,關閉虛擬化的唯一方法是設置'什麼VirtualizingStackPanel .IsVirtualizingProperty'爲false。您可以在完成您的方法後再次打開它。 – Hopeless
您應該只爲您的任務使用底層數據,而不是生成的UI容器。 – metacircle
Uderlying數據是來自SQL數據庫的表數據,即我顯示的列(甚至是鏈表)的更多字段。我的應用程序中有很多ListView,這意味着我將不得不返回到每個視圖並定義應該導出到Excel的內容。所以我正在尋找一種方法來強制ListView通過代碼加載所有項目。 – Karnalta