automatically generate wpf listview columns
Published 8. November 2011
this code generates automatically listview columns for all properties for the given datatype
public class BaseListView : ListView
{
public BaseListView() {
ItemsSourceProperty.AddOwner(typeof(BaseListView), new FrameworkPropertyMetadata(null, OnItemsSourcePropertyChanged));
}
private static void OnItemsSourcePropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) {
if (e.OldValue != e.NewValue && e.NewValue != null) {
var lv = (BaseListView)dependencyObject;
var gridView = new GridView();
lv.View = gridView;
gridView.AllowsColumnReorder = true;
var properties = lv.DataType.GetProperties();
foreach (var pi in properties) {
var binding = new Binding {Path = new PropertyPath(pi.Name), Mode = BindingMode.OneWay};
var gridViewColumn = new GridViewColumn() {Header = pi.Name, DisplayMemberBinding = binding};
gridView.Columns.Add(gridViewColumn);
}
}
}
public Type DataType { get; set; }
}
here is the xaml usage
<Grid>
<local:BaseListView x:Name="listView" DataType="{x:Type DummyType}" ItemsSource="{Binding Mode=OneWay, Path=DummyTypeList}" />
</Grid>
thats it
here is my answer at stackoverflow
Posted in common, develop, stackoverflow, tips&tricks | Tagged .net, develop, listview, stackoverflow, wpf
Leave a Reply Cancel reply
Friends
Latest Tweets
- #Instacanvas ROCKS! A new app for Instagram artists to buy, sell, and discover art. http://t.co/Z86hb7MY #ifttt
- next arduino fun part lcd2004 #arduino #arduinouno http://t.co/QbynHDIZ
- RT @DerBulo: Und hier nochmal der finale Vatertags-Cartoon: http://t.co/WTTDfjIA #ifttt #lol #cartoon #fathersday
- found: MaKey MaKey: how to turn anything into an input-device controller http://t.co/p4BoacNv
- great arduino solar power project ;-) #arduino #arduinouno http://t.co/nKMWlx3O
- found: Using Arduino shields with the Raspberry Pi http://t.co/xPaRDGz7
- RT @Build3dprinter: Re:publica 12: Schluss mit digital, bau dir was! - re:publica - Kultur - Tagesspiegel http://t.co/EL0HwpGB #ifttt
- the #sun between #trees #instagram #instacanvas http://t.co/SkPgZ81U
Shorttags
.net
abo
abofalle
abzocke
application experience
arduino
conference
develop
freeware
garden
gpx
handy
hardware
home
interconmedia
iphone
joggen
kids
listview
lock
music
opendownload
plugin
process
punk
rancid
running
runtastic
ska
software
softwaresammler
stackoverflow
syntaxhighlighter
system
tractor
unlocker
windows
wordpress
work
wpf
Categories
- arduino (1)
- common (13)
- develop (4)
- hardware (1)
- home sweet home (6)
- kids (2)
- music (1)
- running (7)
- software (2)
- stackoverflow (1)
- tips&tricks (3)
- windows (1)
