1

我有一个列表框和一个组合框,描述了下面的 XAML 代码,我试图从 IronPython 代码而不是 XAML 中填充这个列表框和组合框。

如何从代码中填充此列表?

在列表中,我需要多列。

<ComboBox
x:Name="comboBox1"
Grid.Column="0"
Grid.Row="0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="53,14.223,0,0"
Width="54"
Height="19" />

<ListBox
x:Name="listBox1"
Grid.Column="0"
Grid.Row="0"
VerticalAlignment="Top"
Margin="0,30.223,14.5,0"
Height="368.639" HorizontalAlignment="Right" Width="442.619" />
4

1 回答 1

1

使用以下 SO 帖子中接受的答案:如何在 IronPython 中绑定到 ListBox?我设法从 Ironpython 代码中填充和绑定 m 组合框和列表。

我会将所有代码放在这里,以防有人发现自己处于同样的情况:

首先需要更改列表框的 XAML 以指定绑定:

<DataTemplate x:Key="DataTemplate1">
             <Grid>
                  <Grid.ColumnDefinitions>
                     <ColumnDefinition Width="80"/>
                     <ColumnDefinition Width="*"/>
                  </Grid.ColumnDefinitions>
            <TextBlock Text="{Binding Path=lproperty, FallbackValue=Property}" />
            <TextBlock Text="{Binding Path=lvalue, FallbackValue=Value}" Grid.Column="1" HorizontalAlignment="Right" Margin="0,0,-60,0" Width="360" />                

            </Grid>     


</DataTemplate>

那么您还需要将列表框内容绑定到此模板:

<ListBox
                            x:Name="listBox1"
                            Grid.Column="0"
                            Grid.Row="0"
                            VerticalAlignment="Top"
                            Margin="0,30.223,14.5,0"
                            Height="368.639" HorizontalAlignment="Right" Width="442.619" 
                            ItemsSource="{Binding}" ItemTemplate="{DynamicResource DataTemplate1}"/>

我还将把填充组合框和列表框的整个代码放在这里,因为它不是那么大:

import wpf
from System.Windows import Application
from Window1 import Window1
from System.Windows.Controls import(ComboBox, 
    ComboBoxItem, ListBox, ListBoxItem)
from System.Collections.ObjectModel import *
from System.ComponentModel import *
from System.Windows.Controls import *
import pyevent 





entries = {
1 : ('Email', 'test.user@gmail.com' ), 
2 : ('Address', 'new york'),
3 : ('Notes', 'this is a dummy form'), 
4 : ('Mobile Phone', '57234985734'),
5 : ('Work Fax', '5432578943'), 
6 : ('Work Phone', '32465765765') 
}

politetitles = {
1 : ('Mr' ), 
2 : ('Ms'),
3 : ('Mrs'), 
4 : ('Sir'),
}

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

    def add_PropertyChanged(self, value):
        if self.PropertyChanged is not None: 
            self.PropertyChanged += value

    def remove_PropertyChanged(self, value):
        if self.PropertyChanged is not None: 
            self.PropertyChanged -= value

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


class myListEntry(NotifyPropertyChangedBase):

@property
def lvalue(self):
    return self._lvalue

@lvalue.setter
def lvalue(self, value):
    self._lvalue = value
    self.OnPropertyChanged("lvalue")

@property
def lproperty(self):
    return self._lproperty

@lproperty.setter
def lproperty(self, value):
    self._lproperty = value
    self.OnPropertyChanged("lproperty")


window = Window1()

#print window
app = Application()

combo = ComboBox()
titleitems = politetitles.items()
for key, data in titleitems:
    item = ComboBoxItem()
    item.Content = data
    item.FontSize = 8
    combo.Items.Add(item)
window.comboBox1.ItemsSource = combo.Items


listitems = entries.items()
listb = ObservableCollection[myListEntry]()
for key, data in listitems:
    item = ListBoxItem()
    lineitem = myListEntry()
    lineitem.lproperty=data[0]
    lineitem.lvalue=data[1]
    listb.Add(lineitem)
window.listBox1.ItemsSource = listb
print listb
app.Run(window)
于 2012-04-20T10:15:19.937 回答