If you want the columns to be generated dynamically for you in WPF, you need to dynamically add the column definitions with the appropriate bindings each time you view the list view again.
Try the following:
MainWindow.xaml.cs
using System.Collections.Generic; using System.Windows; using System.Windows.Controls; using System.Windows.Data; namespace ListViewTest { public class Column { public string Title { get; set; } public string SourceField { get; set; } }
XAML:
<Window x:Class="ListViewTest.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:ListViewTest" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid> <ListView x:Name="myListView"></ListView> </Grid> </Window>
The only thing you need to change is to add columns from the reader using the column name from the SQL query. And the entry of ExpandoObject from fields returned by the reader from your request.
This is a quick demo that I wrote and did not have access to the local database in order to check it, so that I would simulate it using for loops and an array of strings.
Daniel Wardin
source share