[Python.NET] How to handle INotifyPropertyChanged interface
Hansong Huang
hhspiny at live.com
Thu Mar 31 20:38:38 EDT 2016
I am still trying to figure out how to implement WPF MVVM with Python.Net.Previously, I have successfully used System.Dynamic.ExpandoObject as a ViewModel container
self.window = System.Windows.Markup.XamlReader.Load(outStream)self.window.DataContext = DotNetExpandoObject()
where DotNetExpandoObject is a wrapper class for System.Dynamic.ExpandoObject class DotNetExpandoObject(System.Dynamic.ExpandoObject):... in which I redefined __getattr__ and __setattr__
it works flawlessly as System.Dynamic.ExpandoObject implements INotifyPropertyChange interface already
Now, I would like to get away from using System.Dynamic.ExpandoObject but to implement my own
I first tried to expose a python class back to .Netclass MyViewModel(System.Object): __namespace__ = "WPFPyDemo" def __init__(self): super(MyViewModel,self).__init__() self._inputText = "Line - in" @clr.clrproperty(str) def inputText(self): return self._inputText @inputText.setter def inputText(self,value): self._inputText = value self.OnPropertyChanged("inputText")
self.window.DataContext = MyViewModel()
The one direction data binding works fine, and the "Line - in" text correctly shows up in the textblock control.obviously this is not sufficient as there is no INotifyPropertyChange interface.
So, I inherit the interface , and tries to implement the typical C# code" public event PropertyChangedEventHandler PropertyChanged"
-- not sure how to handle event in Python.Net, the follow code probably is completely wrong
class ViewModel(System.ComponentModel.INotifyPropertyChanged): __namespace__ = "WPFPy" def __init__(self): super(ViewModel, self).__init__() self.PropertyChanged = System.ComponentModel.PropertyChangedEventHandler def OnPropertyChanged(self, propertyName): if self.PropertyChanged != None: PropertyChanged(self, System.ComponentModel.PropertyChangedEventArgs(propertyName))
class MyViewModel(ViewModel): __namespace__ = "WPFPyDemo" def __init__(self): super(MyViewModel,self).__init__() self._inputText = "Line - in"
@clr.clrproperty(str) def inputText(self): return self._inputText @inputText.setter def inputText(self,value): self._inputText = value self.OnPropertyChanged("inputText")
The process terminates due to StackOverflowException. Looks like the error happens when WFP recoganized INotifyPropertyChange interface and tries to access MyViewModel class via it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pythondotnet/attachments/20160331/456edb82/attachment.html>
More information about the PythonDotNet
mailing list