Here are the example files.

Lukas Cenovsky wrote:
Hi Hall,
I'm trying to make Silverlight 3 validation working with IronPython but it seems there is necessary a small manual step.

I followed some tutorials and this should be enough:

1) Set binding to: Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True
2) Raise Exception in the setter

Then the control with bound property should became invalid.

I tried it with TextBox and I failed - the control still appeared valid even when the exception was raised.

The solution is to manually set the control into invalid state (based on BindingValidationError event) - see attached files for example.

Does anybody know why the control is not set into invalid state automatically?

--
-- Lukáš

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



from System.Windows import Application
from System.Windows.Controls import UserControl

import clrtype
import pyevent
from System.ComponentModel import INotifyPropertyChanged, 
PropertyChangedEventArgs
from System.Windows import VisualStateManager
from System.Windows.Controls import ValidationErrorEventAction

class ValidationClass(INotifyPropertyChanged):
    __metaclass__ = clrtype.ClrClass
    PropertyChanged = None

    def __init__(self, win):
        self.win = win
        self._text = 'text'
        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))

    @property
    @clrtype.accepts()
    @clrtype.returns(str)
    def text(self):
        return self._text

    @text.setter
    @clrtype.accepts(str)
    @clrtype.returns()
    def text(self, value):
        if not value.startswith('text'):
            self.win.FindName('tbError').Text = "value error"
            raise Exception('value must start with text')
        self.win.FindName('tbError').Text = ""
        self._text = value
        self.OnPropertyChanged('text')

class App:
    def __init__(self):
        self.root = Application.Current.LoadRootVisual(UserControl(), 
"app.xaml")
        self.root.DataContext = ValidationClass(self.root)
        self.root.BindingValidationError += self.OnBindingValidationError

    def OnBindingValidationError(self, sender, event):
        if event.Action == ValidationErrorEventAction.Added:
            VisualStateManager.GoToState(event.OriginalSource, 
'InvalidUnfocused', True)
        else:
            VisualStateManager.GoToState(event.OriginalSource, 'Valid', True)

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