Hi @curtisw, all, I’m struggling to bind some custom class -CustomObj- properties to gridView DataCells in IronPython.
CustomObj class looks like this:
class CustomObj(System.Object):
def __init__(self,name=None,obj=None,active=False,type=None):
self._name = name
self._obj = obj
self._active = active
self.type = type
@property
def name(self):
return self._name
@name.setter
def name(self,value):
self._name = value
@property
def obj(self):
return self._obj
@obj.setter
def obj(self,value):
self._obj = value
@property
def active(self):
return self._active
@active.setter
def active(self,value):
self._active = value
CustomObjs are stored in an instance of MyModel class, in various properties that are .net BindableCollections.
class MyModel():
def __init__(self):
self.views = System.Collections.ObjectModel.ObservableCollection[type(CustomObj())]()
self.versions = System.Collections.ObjectModel.ObservableCollection[type(CustomObj())]()
def add(self,customObj):
if customObj.type == 'view':
self.views.Add(customObj)
elif customObj.type == 'version':
self.versions.Add(customObj)
I finally create a dialogClass with a gridView, and assign one of the MyModels observableCollections as gridView’s DataStore. Now, I’m struggling to figure out how I can bind a property of each item of the observableCollection to DataCells.
I tried with Binding.Property as follows:
class MyDialog(forms.Dialog[bool]):
def __init__(self,myModel):
self.cm = myModel
self.gridView = forms.GridView()
self.gridView.ShowHeader = True
self.gridView.DataStore = self.cm.views #set a bindableCollection as gridView DataStore
col_01 = forms.GridColumn()
col_01.HeaderText = 'view'
col_01.Editable = False
col_01.DataCell = forms.TextBoxCell(Binding = forms.Binding.Property[type(CustomObj()),System.String]('name')) ##NOT WORKING CODE!!
col_02 = forms.GridColumn()
col_02.Editable = True
col_02.DataCell = forms.CheckBoxCell(Binding=forms.Binding.Property[type(CustomObj()),System.Boolean]('active')) ##NOT WORKING CODE!!
#[code follows...]
![image]()
Alternative, I tried using delegates as the example shared by @dale here . With this tecnique, it seems that TextBoxCell is correctly binding to ‘name’ property, but it fails to bing to the other DataCell of type CheckBoxCell.
class MyDialog(forms.Dialog[bool]):
def __init__(self,myModel):
self.cm = myModel
self.gridView = forms.GridView()
self.gridView.ShowHeader = True
self.gridView.DataStore = self.cm.views #set a bindableCollection as gridView DataStore
col_01 = forms.GridColumn()
col_01.HeaderText = 'view'
col_01.Editable = False
col_01.DataCell = forms.TextBoxCell(Binding = forms.Binding.Delegate[CustomObj,System.String] (self.myNameDelegate)) #THIS BINDING WORKS!!!
col_02 = forms.GridColumn()
col_02.Editable = True
col_02.DataCell = forms.CheckBoxCell(Binding = forms.Binding.Delegate[CustomObj,System.Boolean](self.myActiveDelegate)) #THIS MISERABLY FAILS!!! WHY. THE OTHER BINDING WORKS SO NICELY...
#[code follows... complete code at the end of the post]
def myNameDelegate(self,customObj):
return customObj.name
def myActiveDelegate(self,customObj):
nullableBool = clr.Convert(customObj.active,System.Nullable)
return nullableBool
I really can’t figure out how is done this with IronPython. All examples found out there were for C# and I can’t figure out how to translate them to IronPython.
Any help would be appreciated. Let me share the full code. Thanks!
import System
import Eto.Forms as forms
import Eto.Drawing as drawing
import clr
class CustomObj(System.Object):
def __init__(self,name=None,obj=None,active=False,type=None):
self._name = name
self._obj = obj
self._active = active
self.type = type
@property
def name(self):
return self._name
@name.setter
def name(self,value):
self._name = value
@property
def obj(self):
return self._obj
@obj.setter
def obj(self,value):
self._obj = value
@property
def active(self):
return self._active
@active.setter
def active(self,value):
self._active = value
class MyModel():
def __init__(self):
self.views = System.Collections.ObjectModel.ObservableCollection[type(CustomObj())]()
self.versions = System.Collections.ObjectModel.ObservableCollection[type(CustomObj())]()
def add(self,customObj):
if customObj.type == 'view':
self.views.Add(customObj)
elif customObj.type == 'version':
self.versions.Add(customObj)
class MyDialog(forms.Dialog[bool]):
def __init__(self,myModel):
self.cm = myModel
self.gridView = forms.GridView()
self.gridView.ShowHeader = True
self.gridView.DataStore = self.cm.views #set a bindableCollection as gridView DataStore
col_01 = forms.GridColumn()
col_01.HeaderText = 'view'
col_01.Editable = False
###binding to property trial...###
#col_01.DataCell = forms.TextBoxCell(Binding = forms.Binding.Property[type(CustomObj()),System.String]('name'))
###binding to delegate trial...###
col_01.DataCell = forms.TextBoxCell(Binding = forms.Binding.Delegate[CustomObj,System.String](self.myNameDelegate)) ## THIS IS WORKING CODE!!
col_02 = forms.GridColumn()
col_02.Editable = True
###binding to property trial...###
#col_02.DataCell = forms.CheckBoxCell(Binding=forms.Binding.Property[type(CustomObj()),System.Boolean]('active')) ##NOT WORKING CODE!!
###binding to delegate trial...###
#col_02.DataCell = forms.CheckBoxCell(Binding = forms.Binding.Delegate[CustomObj,System.Boolean](self.myActiveDelegate))
self.gridView.Columns.Add(col_01)
self.gridView.Columns.Add(col_02)
addButton = forms.Button(Text = 'test Add Element')
def onAddButtonClick(sender,e):
try:
self.cm.add(CustomObj(name = 'testName',active = True,type='view'))
self.veriLabel.Text = 'number of elements: {0}'.format(str(self.cm.views.Count))
except Exception as es:
Rhino.RhinoApp.WriteLine(str(e))
addButton.Click += onAddButtonClick
self.veriLabel = forms.Label(Text = 'number of elements: {0}'.format(str(self.cm.views.Count)))
self.layout = forms.TableLayout(
Padding=drawing.Padding(5),
Size = drawing.Size(500,500)
)
self.layout.Rows.Add(forms.TableRow(self.gridView))
self.layout.Rows.Add(forms.TableRow(addButton))
self.layout.Rows.Add(forms.TableRow(self.veriLabel))
self.Content = self.layout
def myNameDelegate(self,customObj):
return customObj.name
def myActiveDelegate(self,customObj):
nullableBool = clr.Convert(customObj.active,System.Nullable)
return nullableBool
#CREATE ANY MODEL
myModel = MyModel()
for i in range(10):
name = 'name_{0}'.format(i)
if i>5:
active = False
else:
active = True
myModel.add(CustomObj(name=name, active=active,type='view'))
#LAUNCH DIALOG
myDlg = MyDialog(myModel)
myDlg.ShowModal()
```
3 posts - 1 participant
Read full topic