WPF 和 Python:从数据模板获取复选框值

问题描述 投票:0回答:0

我不确定这是不是问这个问题的正确地方,但这里什么也没有:

我正在用 Python 中的代码创建一个 wpf 窗口。我似乎无法弄清楚如何获取我的组合框的选定项目(它们在 DataTemplate 中)。
这是我目前所拥有的,但我似乎无法让 show_result 功能发挥作用。有没有人有这方面的经验?

XML:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="450" MinHeight="450" Width="800" MinWidth="600">
    <Window.Resources>
        <DataTemplate x:Key="DataTemplate1" x:Name="DataTemplate1">
            <Grid Height="auto" x:Name="GridTemplate">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto"/>
                </Grid.RowDefinitions>
                <ComboBox ItemsSource="{Binding Area}" x:Name="Area1" Margin="3" Grid.Column="0" Grid.Row="1"/>
                <ComboBox ItemsSource="{Binding Verdieping}" x:Name="Verdieping1" Margin="3" Grid.Column="1" Grid.Row="1"/>
                <ComboBox ItemsSource="{Binding Gevel}" x:Name="Gevel1a" Margin="3" Grid.Column="2"/>
                <ComboBox ItemsSource="{Binding Gevel}" x:Name="Gevel1b" Margin="3" Grid.Column="3"/>
                <ComboBox ItemsSource="{Binding Gevel}" x:Name="Gevel1c" Margin="3" Grid.Column="4"/>
                <ComboBox ItemsSource="{Binding Gevel}" x:Name="Gevel1d" Margin="3" Grid.Column="5"/>
            </Grid>
        </DataTemplate>
    </Window.Resources>
    <StackPanel Margin="10">
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
            <Label FontWeight ="Bold" Content="Title Block"/>
            <ComboBox x:Name="TitleBlockComboBox" Margin="57,3,3,3" MinWidth="200" HorizontalAlignment="Left"/>
            <Label FontWeight="Light" Content=">  selecteer hier een horizontale A3-sheet" Margin="10,0,0,0" Foreground="#7F000000"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
            <Label FontWeight ="Bold" Content="View Template"/>
            <ComboBox x:Name="ViewTemplateComboBox" Margin="33,3,3,3" MinWidth="200" HorizontalAlignment="Left"/>
            <Label FontWeight="Light" Content=">  view template voor het verdieping plan" Margin="10,0,0,0" Foreground="#7F000000"/>
        </StackPanel>
        <Separator Height="5"/>
        <Border Margin="0,10" Background="AliceBlue" CornerRadius="10">
            <StackPanel x:Name="StackPanel1" Margin="5">
                <Grid Height="auto" x:Name="Grid1">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="1*"/>
                    </Grid.RowDefinitions>
                    <Label Grid.Column="0" Grid.Row="0" FontWeight ="Bold" Content="Area"/>
                    <Label Grid.Column="1" Grid.Row="0" FontWeight ="Bold" Content="Verdieping plan"/>
                    <Label Grid.Column="2" Grid.Row="0" FontWeight ="Bold" Content="Gevel 1"/>
                    <Label Grid.Column="3" Grid.Row="0" FontWeight ="Bold" Content="Gevel 2"/>
                    <Label Grid.Column="4" Grid.Row="0" FontWeight ="Bold" Content="Gevel 3"/>
                    <Label Grid.Column="5" Grid.Row="0" FontWeight ="Bold" Content="Gevel 4"/>
                </Grid>
            </StackPanel>
        </Border>
        <StackPanel Orientation="Horizontal">
            <Button x:Name="AddRowButton" Content="Add Row" Click="AddRow_Click" Margin="0,5,5,5" Width="200"/>
            <Button x:Name="RemoveRowButton" Content="Remove Row" Click="RemoveRow_Click" Margin="5,5,0,5" Width="200"/>
        </StackPanel>
        <Button x:Name="show_result" Content="toon resultaat" Click="show_result" Margin="0,5"/>
    </StackPanel>
</Window>

蟒蛇

### IMPORTS ###
from pyrevit import script
from pyrevit import forms
from System.Windows import *
from System.Windows.Controls import *
from rpw.ui.forms import Alert

### GLOBAL PARAMETERS (and temporary lists)###
xamlfile = script.get_bundle_file('ui.xaml')
countCombo = 0
countRows = 0
areasTemp = ["area1", "area2", "area3"]
verdTemp = ["verd0", "verd1", "verd2", "verd3", "verd4"]
gevelTemp = ["gevel1", "gevel2", "gevel3", "gevel4"]
titleBlocksDict = ["tb1", "tb2", "tb3", "tb4", "tb5", "tb6"]
viewTemplatesDict = ["vt1", "vt2", "vt3"]

### DEFINITIONS FOR THE ITEMSOURCES ###
class WPFItems:
    def __init__(self, area, verdieping, gevel):
        self.Area = area
        self.Verdieping = verdieping
        self.Gevel = gevel

GridTemplateData1 = WPFItems(areasTemp, verdTemp, gevelTemp)

### DEFINITIONS TO ADD/REMOVE ROWS ###

def add_row(window):
    global countRows
    countRows += 1
    cc = ContentControl()
    cc.Name = "contentControl{}".format(countRows)
    cc.ContentTemplate = window.Resources["DataTemplate1"]
    cc.Content = GridTemplateData1
    window.StackPanel1.Children.Add(cc)
    return cc

def remove_row(window):
    global countRows
    if countRows > 1:
        countRows -= 1
    num_children = len(window.StackPanel1.Children)
    if num_children > 2:
        last_child = window.StackPanel1.Children[num_children - 1]
        window.StackPanel1.Children.Remove(last_child)

### CREATE THE WINDOW CLASS ###
class MyWindow(forms.WPFWindow):
    def __init__(self):
        forms.WPFWindow.__init__(self, xamlfile)
        self.comboboxes = []
        self.rows = []
        row = add_row(self)
        self.rows.append(row)
        
        #default values
        self.TitleBlockComboBox.ItemsSource = titleBlocksDict
        self.ViewTemplateComboBox.ItemsSource = viewTemplatesDict
    
    def AddRow_Click(self, sender, e):
        row = add_row(self)
        self.rows.append(row)
    
    def RemoveRow_Click(self, sender, e):
        remove_row(self)
        if len(self.rows) > 1:
            del self.rows[-1]
    
    def show_result(self, sender, e):
        result = []
        for row in self.rows:
            temp = row.ContentTemplate.FindName("Area1", row)
            result.append(temp)
        Alert(str(result), "geselecteerde waardes")

### SHOW WINDOW ###
if __name__ == '__main__':
    my_window = MyWindow()
    my_window.ShowDialog()

当我只使用组合框时,我可以轻松获得“SelectedItem”。但是,现在我使用的是 ContentControl,我找不到在其中获取该组合框的方法。

python wpf user-interface revit-api pyrevit
© www.soinside.com 2019 - 2024. All rights reserved.