Hi all,
when working with DataGrid in WPF application, I have found two bugs. I am not sure they are IronPython bugs but I don't know how to investigate more - I am not so fluent in WinDbg :-)

The first (more severe) bug is the /Object reference not set to an instance of an object/ error. The attached sample consists of DataGrid with some items and property /sel_item/ that is bound to DataGrid.SelectedItem. When you try to disable the whole DataGrid (click the button), the /Object reference not set to an instance of an object/ appears:

Traceback (most recent call last):
 File "C:\IronPython-2.6.1\wpf.grid.py", line 109, in <module>
 File "C:\IronPython-2.6.1\wpf.grid.py", line 103, in btnClick
SystemError: Object reference not set to an instance of an object.>>>

It only appears when you use binding to DataGrid.SelectedItem. If you remove the binding (lines 21-22; in xaml), no error appears.

The second error appears when you initialize /sel_item/ to None (change line 73 to: self.sel_item = None). The DataGrid has initially no selected row. When you click on any, the binding error appears:

TypeConverter cannot convert from DataItem.

I guess this is more likely to be correct behavior because the Binding is initialized with None instead of the row type so it has no way to find out the right conversion.

I use IronPython 2.6.1 and WFP Toolkit from February 2010.

It would be nice if somebody could check these bugs.  Thanks.

--
-- Lukáš
import clr
import clrtype
import pyevent
clr.AddReference('PresentationFramework')
clr.AddReference('PresentationCore')
clr.AddReference('WindowsBase')
clr.AddReference('WPFToolkit')
from System.Windows import Application
from System.ComponentModel import INotifyPropertyChanged, 
PropertyChangedEventArgs
from System.Collections.ObjectModel import ObservableCollection
from System.Windows.Markup import XamlReader
from System.Windows.Controls import Validation
from System.Windows import RoutedEventHandler

xaml = """ <Window Width="200" Height="200"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation";
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml";
  xmlns:data="http://schemas.microsoft.com/wpf/2008/toolkit";>
  <StackPanel>
    <data:DataGrid x:Name="grdItems" ItemsSource="{Binding data}"
        SelectedItem="{Binding sel_item, Mode=TwoWay,
        ValidatesOnExceptions=True, NotifyOnValidationError=True}"/>
    <TextBlock Text="{Binding sel_item.value}" HorizontalAlignment="Center" />
    <Button x:Name="btn" Width="150" Height="23" Content="Disable grid"/>
  </StackPanel>
</Window>"""

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 DataItem(NotifyPropertyChangedBase):
    __metaclass__ = clrtype.ClrClass

    def __init__(self, value):
        super(DataItem, self).__init__()
        self.value = value

    @property
    @clrtype.accepts()
    @clrtype.returns(str)
    def value(self):
        return self._value

    @value.setter
    @clrtype.accepts(str)
    @clrtype.returns()
    def value(self, value):
        self._value = value
        self.OnPropertyChanged('value')

class ModelClass(NotifyPropertyChangedBase):
    __metaclass__ = clrtype.ClrClass

    def __init__(self):
        super(ModelClass, self).__init__()
        self._data = ObservableCollection[DataItem]()
        self.data.Add(DataItem('one'))
        self.data.Add(DataItem('two'))
        self.data.Add(DataItem('three'))
        self.sel_item = self.data[1]

    @property
    @clrtype.accepts()
    @clrtype.returns(DataItem)
    def sel_item(self):
        return self._sel_item

    @sel_item.setter
    @clrtype.accepts(DataItem)
    @clrtype.returns()
    def sel_item(self, value):
        self._sel_item = value
        self.OnPropertyChanged('sel_item')

    @property
    @clrtype.accepts()
    @clrtype.returns(ObservableCollection[DataItem])
    def data(self):
        return self._data

class MyApp:
    def __init__(self):
        self.root = XamlReader.Parse(xaml)
        self.root.DataContext = ModelClass()
        self.root.AddHandler(Validation.ErrorEvent,
                RoutedEventHandler(self.OnBindingValidationError))
        self.root.FindName('btn').Click += self.btnClick

    def btnClick(self, sender, event):
        self.root.FindName('grdItems').IsEnabled = False

    def OnBindingValidationError(self, sender, event):
        print event.Error.ErrorContent

app = Application()
app.Run(MyApp().root)
_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Reply via email to