WPF Table View
import clr
clr.AddReference("PresentationFramework")
clr.AddReference("PresentationCore");
clr.AddReference('WindowsBase')
clr.AddReference('System.Data')
clr.AddReference('System.ComponentModel')
clr.AddReference('System.Windows.Forms')
import System
from System import *
from System.Data import *
from System.Type import GetType
from System.Threading.Tasks import Task
from System.ComponentModel import *
from System.Windows import *
from System.Windows.Controls import *
from System.Windows.Controls.Primitives import *
from System.Windows.Media import *
from System.Windows.Media.Imaging import *
from System.Windows.Media.Effects import *
from System.Windows.Shapes import Rectangle
from System.Windows.Data import *
from System.Windows.Ink import Stroke
from System.Windows.Forms import ( FolderBrowserDialog, DialogResult )
from Microsoft.Win32 import ( OpenFileDialog, SaveFileDialog )
import ctrl
import status
class WpfTable(ctrl.WpfCtrl):
def __init__(self,handler=None,columns=None,widths=None,**kwargs):
self.ctrl = ListView()
self.table = DataTable('table')
self.cols = []
self.init(**kwargs)
if handler: self.ctrl.SelectionChanged += handler
self.grid = GridView()
self.grid.AllowsColumnReorder = True;
self.grid.ColumnHeaderToolTip = "ListView Column Info";
if columns:
for i in range(0,len(columns)):
width = widths[i] if widths else None
self.add_column(columns[i],width)
self.ctrl.View = self.grid
self.ctrl.ItemsSource = self.table.DefaultView #DataView(self.table)
self.ctrl.AddHandler(GridViewColumnHeader.ClickEvent, RoutedEventHandler(self.OnColumnHeaderClick))
self.sort_dir = ListSortDirection.Ascending
def clear(self):
#k = self.table.Rows.IndexOf(item)
for k in range(len(self.table.Rows),0,-1):
self.table.Rows[k-1].Delete()
self.table.AcceptChanges() #RejectChanges()
def add_column(self,name,width=None,label=None):
col = GridViewColumn()
if width: col.Width = width
col.Header = label if label else name
col.DisplayMemberBinding = Binding(name)
self.grid.Columns.Add(col);
item = DataColumn(name, GetType("System.String"))
self.table.Columns.Add(item)
self.cols.append(name)
def add_row(self,row):
if len(self.cols) == len(row):
item = self.table.NewRow()
for index in range(len(row)):
item[self.cols[index]] = row[index]
self.table.Rows.Add(item)
def add_item(self,items):
item = self.table.NewRow()
if type(items) == dict:
for key, value in items.items(): item[key] = value;
else:
for i in range(len(items)): item[i] = items[i];
self.table.Rows.Add(item);
def get_value(self):
return self.ctrl.SelectedItem
def get_selected_items_org(self):
return self.ctrl.SelectedItems
def get_selected_items(self):
items = []
for row in self.ctrl.SelectedItems:
item = []
for col in self.cols:
item.append(row[col])
items.append(item)
return items
def get_all_items(self):
items = []
for row in self.ctrl.Items:
item = []
for col in self.cols:
item.append(row[col])
items.append(item)
return items
def get_column_items(self,col):
items = []
for row in self.ctrl.Items:
items.append(row[col])
return items
def AddSortColumnName(self,col_name,ascending=True):
view = CollectionViewSource.GetDefaultView(self.ctrl.ItemsSource)
if ascending:
view.SortDescriptions.Add(SortDescription(col_name, ListSortDirection.Ascending))
else:
view.SortDescriptions.Add(SortDescription(col_name, ListSortDirection.Descending))
def column_sort(self,col_name,direction):
view = CollectionViewSource.GetDefaultView(self.ctrl.ItemsSource)
view.SortDescriptions.Clear()
view.SortDescriptions.Add(SortDescription(col_name, direction))
view.Refresh()
def OnColumnHeaderClick(self,sedner,event):
headerClicked = event.OriginalSource
columnBinding = headerClicked.Column.DisplayMemberBinding
if self.sort_dir == ListSortDirection.Descending:
self.sort_dir = ListSortDirection.Ascending
headerClicked.Column.Header = columnBinding.Path.Path + " (+)"
else:
self.sort_dir = ListSortDirection.Descending
headerClicked.Column.Header = columnBinding.Path.Path + " (-)"
self.column_sort( columnBinding.Path.Path, self.sort_dir ) #headerClicked.Column.Header
class WpfTable0(ctrl.WpfCtrl):
def __init__(self,handler=None,columns=None,widths=None,**kwargs):
self.ctrl = ListView()
self.table = DataTable('table')
self.init(**kwargs)
if handler: self.ctrl.SelectionChanged += handler
self.grid = GridView()
self.grid.AllowsColumnReorder = True;
self.grid.ColumnHeaderToolTip = "ListView Column Info";
if columns:
for i in range(0,len(columns)):
col = GridViewColumn()
col.DisplayMemberBinding = Binding(columns[i])
col.Header = columns[i]
if widths: col.Width = widths[i]
self.grid.Columns.Add(col);
self.add_column(columns[i])
self.ctrl.View = self.grid
self.ctrl.ItemsSource = self.table.DefaultView #DataView(self.table)
def add_column(self,label):
item = DataColumn(label, GetType("System.String"))
self.table.Columns.Add(item)
def add_item(self,items):
item = self.table.NewRow()
if type(items) == dict:
for key, value in items.items(): item[key] = value;
else:
for i in range(len(items)): item[i] = items[i];
self.table.Rows.Add(item);
def get_value(self):
return self.ctrl.SelectedItem
def get_selected_items(self):
return self.ctrl.SelectedItems
if __name__ == "__main__":
global table_ctrl
def table_handler(s,e):
row = s.SelectedItem
print( row['1st'], row['2nd'], row['3rd'] )
def print_handler(s,e):
rows = table_ctrl.get_selected_items()
for row in rows:
print(row['1st'], row['2nd'], row['3rd'])
def add_handler(s,e):
table_ctrl.add_item( ( "aaa1", "aaa2", "aaa3" ) )
table_ctrl.add_item( {"2nd": "bbb2","3rd":"bbb3","1st":"bbb1"} )
table_ctrl.add_item( ( "ccc1", "ccc2", "ccc3" ) )
def clear_handler(s,e):
table_ctrl.clear()
import win
app = win.Win("Button Demo",320,240)
table_ctrl = WpfTable(columns=("1st","2nd","3rd"),
widths=(50,150,120),
handler=table_handler)
table_ctrl.add_item( ( "aaa1", "aaa2", "aaa3" ) )
table_ctrl.add_item( {"2nd": "bbb2","3rd":"bbb3","1st":"bbb1"} )
table_ctrl.add_item( ( "ccc1", "ccc2", "ccc3" ) )
app.set_content(table_ctrl.ctrl)
sb = status.WpfStatusBar()
sb.add_spacer()
sb.add_button( "Add", add_handler )
sb.add_button( "Clear", clear_handler )
app.add_statusbar(sb.ctrl)
app.run()