请耐心等待,我是 GUI 编程、IronPython、WPF 和 .NET 的新手。但是,我对 Python 相当熟悉。我已经阅读了许多有关数据绑定的教程,例如 DevHawk,但这些对我来说太高级了。
问题:我想在 Listbox 中显示可以修改、添加或删除条目的 python 列表中的文件路径。我按照教程完成的第一部分。
但是,当我更新文件列表时,列表框控件不会使用新文件进行更新,并且当我尝试操作列表框时,应用程序会出现以下来自 VS2012 的调试消息:
添加文件 点击 ['C:\test\Employment_Law_Alert_03_28_2012.pdf', 'C:\test\Graph Paper .10in Cartesian ci-110.pdf', 'C:\test\Greek Alphabet Symbol.pdf', 'C:\ test\CNN 小费指南 - 小费多少.pdf']
Traceback(最近一次调用最后一次):文件“”,第 1 行,在 SystemError 中:一个 ItemsControl 与其项目源不一致。有关更多信息,请参阅内部异常。
显然,listbox.ItemSource 与 python 列表不同步。一个如何更新两个?
以下是示例代码...
Python:
def addFile(self, sender, e):
... # Some files added to self.fileInList
self.listBoxPDFs.ItemsSource = self.fileInList
XAML:
<ListBox x:Name="listBoxPDFs" Margin="2, 2, 2, 2">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding BindsDirectlyToSource=True}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
更新:我找到了一种解决方法,但肯定有更好的方法吗?
def addFile(self, sender, e):
... # Some files added to self.fileInList
self.listBoxPDFs.ItemsSource = [] # Clear the listbox first.
self.listBoxPDFs.ItemsSource = self.fileInList # Then rebind.