如何绑定到 IronPython 中的 ListBox? [英] How do I bind to a ListBox in IronPython?

查看:21
本文介绍了如何绑定到 IronPython 中的 ListBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始将 IronPython 与 WPF 一起使用,我不明白应该如何完成绑定.

I am just starting out using IronPython with WPF and I don't quiet understand how binding is supposed to be done.

通常在 WPF 中我会这样做:

Normally in WPF I would just do something like this:

<ListBox Name="MyListBox">
    <ListBox.Resources>
        <Style TargetType="ListBoxItem">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <DockPanel>
                            <TextBlock Text="{Binding Path=From}" />
                            <TextBlock Text="{Binding Path=Subject}" />
                        </DockPanel>
                     </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.Resources>
</ListBox>

然后在我的代码后面:

MyListBox.ItemsSource = new ObservableCollection<Email>()

但在 IronPython 中,我们不能有对象的 ObservableCollection,只有类型.这不起作用:

But in IronPython we cannot have an ObservableCollection of objects, only types. This does not work:

MyListBox.ItemsSource = new ObservableCollection[email]()

当它抛出异常时:expected Array[Type], got classobj"

As it throws the Exception: "expected Array[Type], got classobj"

我该怎么办?请帮忙!

推荐答案

我自己解决了这个问题,我有一些错误,也错过了一些关键点.我希望这个答案可以帮助其他人.

I worked this out myself, I had a few things wrong and was missing a few key point as well. I hope this answer can help someone else.

首先,您需要 IronPython 目录中 tutorial/目录中的 pyevent.py.

First was that you need pyevent.py from the tutorial/ directory in your IronPython directory.

其次,我们需要一个辅助类:

Second we need a helper class:

class NotifyPropertyChangedBase(INotifyPropertyChanged):
    """INotifyProperty Helper"""
    PropertyChanged = None
    def __init__(self):
        (self.PropertyChanged, self._propertyChangedCaller) = make_event()

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

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

    def OnPropertyChanged(self, propertyName):
        self._propertyChangedCaller(self, PropertyChangedEventArgs(propertyName))

然后你需要像这样声明你的数据类:

Then you need to declare your data class like so:

class Email(NotifyPropertyChangedBase):
    """
        use setter getter.
        IronPython 2.6 or later.
    """
    @property
    def From(self):
        return self._From

    @From.setter
    def From(self, value):
        self._From = value
        self.OnPropertyChanged("From")

    @property
    def Subject(self):
        return self._Subject

    @Subject.setter
    def Subject(self, value):
        self._Subject = value
        self.OnPropertyChanged("Subject")

最后设置ListBox的ItemSource:

Finally set the ListBox's ItemSource:

self.data = ObservableCollection[Email]()
self.MyListBox.ItemsSource = self.data

感谢此链接的帮助:http://palepoli.skr.jp/wp/2009/06/28/wpf-listview-databinding-for-ironpython/

这篇关于如何绑定到 IronPython 中的 ListBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆