我有一些来自python脚本的数据集,我希望使用WPF进行可视化。我的布局由多个动态生成的ListBox组成(通过按下' +'按钮添加一个新的:
我设法将ListBoxes本身绑定到ObservableCollection
并添加新元素工作正常。但是,我无法找到如何使用值填充这些ListBox? (假设值是列表中的字符串或简单的dicts)。我认为它需要在我的HDBox
类中完成,但我不知道如何从该类中访问XAML元素等。
XAML:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WpfApplication1" Height="300" Width="500">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30px"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="25px"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Row="1" Orientation="Horizontal" Margin="0,0,0,10" Grid.Column="0" >
<ItemsControl Name="stHDBox">
<ItemsControl.ItemTemplate >
<DataTemplate>
<DockPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto">
<ListBox Name="roomEntries" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MinWidth="200">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Entry}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DockPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</StackPanel>
<Button x:Name="AddHDButton" Content="+" Margin="0,0,0,10" Grid.Row="1" Click="AddHDButton_Click" HorizontalAlignment="Right" Width="24" Grid.Column="1" />
</Grid>
</Window>
Python代码:
import wpf
from System.Windows import Application, Window
from System.Collections.ObjectModel import ObservableCollection
class HDBox:
index = 0
def __init__(self):
# Not sure about that part, doesn't seem to work
rooms = ObservableCollection[type("")]()
self.roomEntries.ItemsSource = rooms
class MyWindow(Window):
def __init__(self):
# Create 1 HDBox by default
self.hDBoxes = ObservableCollection[type(HDBox)]()
self.hDBoxes.Add(HDBox)
self.hDBoxes[0].index = 0
wpf.LoadComponent(self, 'WpfApplication1.xaml')
self.stHDBox.ItemsSource = self.hDBoxes
# '+' Button to add new boxes
def AddHDButton_Click(self, sender, e):
i = len(self.hDBoxes)
self.hDBoxes.Add(HDBox)
self.hDBoxes[i].index = i
if __name__ == '__main__':
Application().Run(MyWindow())
修改 由于正在进行一些编辑,我在这里提供了当前的python代码:
import wpf
from System.Windows import Application, Window
from System.Collections.ObjectModel import ObservableCollection
class HDBox:
index = 0
def __init__(self):
self.prop = ObservableCollection[type("")]()
self.prop.Add("test")
@property
def YourProperty(self):
return self.prop
@YourProperty.getter
def YourPropertyGetter(self):
return self.prop
class MyWindow(Window):
def __init__(self):
# Create 1 HDBox by default
self.hDBoxes = ObservableCollection[type(HDBox)]()
self.hDBoxes.Add(HDBox)
self.hDBoxes[0].index = 0
wpf.LoadComponent(self, 'WpfApplication1.xaml')
self.stHDBox.ItemsSource = self.hDBoxes
# '+' Button to add new boxes
def AddHDButton_Click(self, sender, e):
i = len(self.hDBoxes)
self.hDBoxes.Add(HDBox)
self.hDBoxes[i].index = i
if __name__ == '__main__':
Application().Run(MyWindow())
答案 0 :(得分:1)
HDBox
类应具有ObservableCollection<T>
属性,您可以将ItemsSource
中ListBox
的{{1}}属性绑定到:
ItemTemplate
然后,您可以通过向相应的<ListBox Name="roomEntries" ItemsSource="{Binding YourProperty}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MinWidth="200">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Entry}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
集合属性添加项目来为每个ListBox
填充项目。