首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从python类为WPF设置多个ItemsSource

从python类为WPF设置多个ItemsSource
EN

Stack Overflow用户
提问于 2020-07-25 05:04:33
回答 1查看 197关注 0票数 0

大家好,我正在尝试使用PyRevit在一个WPF应用程序中填充ComboBox和ListBox。这个应用程序运行得很好,其中一个WPF“Box”获得了一个列表,但当我试图添加这两个列表时,我得到了一个错误,说“意外缩进”。以下是我正在努力实现的目标:

代码语言:javascript
复制
# dependencies

import os.path as op
import codecs
from collections import namedtuple

from System.Collections.Generic import List
from collections import OrderedDict
from operator import getitem

from pyrevit import HOST_APP
from pyrevit import USER_DESKTOP
from pyrevit import framework
from pyrevit.framework import Windows, Drawing, ObjectModel, Forms
from pyrevit import coreutils
from pyrevit import forms
from pyrevit import revit, DB
from pyrevit import script
import sys
import threading
from pyrevit import EXEC_PARAMS

import clr
clr.AddReference('System.Windows.Forms')
clr.AddReference('IronPython.Wpf')

from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Architecture import *
from Autodesk.Revit.DB.Analysis import *

# find the path of ui.xaml
from pyrevit import UI

doc = __revit__.ActiveUIDocument.Document

#loggers
logger = script.get_logger()
output = script.get_output()

#Collectors
cmbPrimary = []
lbxViews = []
viewPortList = []
lbxViewsSorted = []

#Get all viewports in Document
viewPorts = list(DB.FilteredElementCollector(doc).OfClass(Viewport))


#Get all Views on sheets, their Name, Sheet Number and Box Outline
for vp in viewPorts:
    sheet = doc.GetElement(vp.SheetId)
    view = doc.GetElement(vp.ViewId)
    vbox = vp.GetBoxOutline()
    viewPortList.append([sheet.SheetNumber, view.ViewName, vbox])

lbxViewsSorted = sorted(viewPortList, key=lambda x: x[0])

for vp in lbxViewsSorted:
    lbxViews.append(vp[0] + " , " + vp[1])
    cmbPrimary.append(vp[0] + " , " + vp[1])

#Create View Model
class ViewModel(forms.Reactive):
    def __init__(self): 
        self.ViewPorts = lbxViews
        self.primaryView = cmbPrimary

# code for the window
class MyWindow(forms.WPFWindow, forms.Reactive):
    def __init__(self):
        self.vm = ViewModel()
    
    def setup(self):
        self.lbxViews.ItemsSource = self.vm.ViewPorts
        self.cmbPrimary.ItemsSource = self.vm.primaryView

# init ui
ui = script.load_ui(MyWindow(), 'ui.xaml')
# show modal or nonmodal
ui.show_dialog()

这只在我尝试填充一个WPF容器时才起作用,下面只是ListBox而不是ComboBox:

代码语言:javascript
复制
def setup(self):
    self.lbxViews.ItemsSource = self.vm.ViewPorts

XAML如下:

代码语言:javascript
复制
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Align Views"
        Width="350" ResizeMode="NoResize" Height="800">
    <StackPanel Margin="10,10,10,10">
        <Label Content="Select view to align to:" Margin="0,0,0,10" />
        <ComboBox x:Name="cmbPrimary" ItemsSource="{Binding cmbPrimary}"/>
        <Label Content="Select views to align:" Margin="0,0,0,10" />
        <ListBox x:Name="lbxViews" Height="450" ItemsSource="{Binding lbxViews}" SelectionMode="Extended" />
        <Label Content="Select alignment point:" Margin="0,0,0,10" />
        <ComboBox x:Name="cmbAlignment"/>
        <StackPanel Margin="8" Orientation="Horizontal">
            <Button x:Name="btnOK" MinWidth="93">OK</Button>
            <Button x:Name="btnCancel" MinWidth="93" Margin="10,0,0,0">Cancel</Button>
        </StackPanel>
    </StackPanel>
    
</Window>

我想我可能需要使用DataContext?但不确定是pythonic问题还是WPFonic问题。任何帮助都将不胜感激!谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-10 01:54:05

我想通了。代码没问题,我有一个不必要的缩进

代码语言:javascript
复制
def setup

一切都好!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63081433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档