Following my binding experiments in WPF, I tried to convert the sample into Silverlight and I hit another binding errors there.

When I bind anything else than string, I receive an error when the target value is changed and the source should be updated. This happens in all IronPython versions (2.6.1 on .Net 2.0 and 4, 2.7a1).

Here is the code I use (the whole source is attached):

    @property
    @clrtype.accepts()
    @clrtype.returns(System.Nullable[System.Boolean])
    def filtered(self):
        return self._filtered

    @filtered.setter
    @clrtype.accepts(System.Nullable[System.Boolean])
    @clrtype.returns()
    def filtered(self, value):
        self._filtered = value
        self.OnPropertyChanged('filtered')

As you can see I tried to use System.Nullable[System.Boolean] instead of bool but it did not help. I also tried to use converter and convert the value to this type (System.Nullable[System.Boolean](value)) but that did not help either. All resulted in the following exception for bool type.

System.Windows.Data Error: Cannot save value from target back to source. BindingExpression: Path='filtered' DataItem='Mountains' (HashCode=2058916); target element is 'System.Windows.Controls.CheckBox' (Name=''); target property is 'IsChecked' (type 'System.Nullable`1[System.Boolean]').. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Security.VerificationException: Operation could destabilize the runtime.
   at Mountains.filtered(Nullable`1 value)
   --- End of inner exception stack trace ---
at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture) at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
   at System.Windows.CLRPropertyListener.set_Value(Object value)
   at System.Windows.PropertyAccessPathStep.set_Value(Object value)
   at System.Windows.Data.BindingExpression.UpdateValue().

The similar exception is raised for float or System.Double.

I have found an old issue similar to this: http://ironpython.codeplex.com/workitem/16831?ProjectName=ironpython

Any workaround would be nice for this bug :-)

--
-- Lukáš
import clr
import clrtype
import pyevent
import System
from System.Windows import Application
from System.Windows.Controls import UserControl
from System.ComponentModel import INotifyPropertyChanged, 
PropertyChangedEventArgs
from System.Collections.ObjectModel import ObservableCollection
from System.Windows.Data import CollectionViewSource, IValueConverter
from System.Windows.Markup import XamlReader

class NotifyPropertyChangedBase(INotifyPropertyChanged):
    PropertyChanged = None

    def __init__(self):
        self.PropertyChanged, self._propertyChangedCaller = pyevent.make_event()

    def add_PropertyChanged(self, value):
        self.PropertyChanged += value

    def remove_PropertyChanged(self, value):
        self.PropertyChanged -= value

    def OnPropertyChanged(self, propertyName):
        if self.PropertyChanged is not None:
            self._propertyChangedCaller(self, 
PropertyChangedEventArgs(propertyName))

class Mountains(NotifyPropertyChangedBase):
    __metaclass__ = clrtype.ClrClass

    def __init__(self):
        super(Mountains, self).__init__()
        self.filtered = True

    @property
    @clrtype.accepts()
    @clrtype.returns(str)
    def filtered_text(self):
        return 't' if getattr(self, 'filtered', False) else 'f'

    @property
    @clrtype.accepts()
    @clrtype.returns(System.Nullable[System.Boolean])
    def filtered(self):
        return self._filtered

    @filtered.setter
    @clrtype.accepts(System.Nullable[System.Boolean])
    @clrtype.returns()
    def filtered(self, value):
        self._filtered = value
        self.OnPropertyChanged('filtered')

class App:
    def __init__(self):
        self.root = Application.Current.LoadRootVisual(UserControl(), 
"app.xaml")
        self.root.FindName('LayoutRoot').DataContext = Mountains()

a = App()

Attachment: app.xaml
Description: application/xaml

_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Reply via email to