2012-04-19 61 views
0

我有一个列表框和一个组合框描述了下面的XAML代码,我想从IronPython代码而不是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" /> 

回答

1

从以下SO后使用公认的答案:How do I bind to a ListBox in IronPython?我设法填充和绑定米IronPython的代码组合框和列表。

我会在这里把所有的代码的情况下,任何人找到他/她自己在同样的情况:

首先是指定绑定需要在XAML变化为ListBox的:

<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}"/> 

我会把也是在这里,填充组合框和列表框的整个代码ssince是第n那么大:

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', '[email protected]'), 
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)