从 python class 为 WPF 设置多个 ItemsSource
Setting multiple ItemsSource for WPF from python class
您好,我正在尝试使用 PyRevit 在 WPF 应用程序中填充 ComboBox 和 ListBox。该应用程序运行良好,其中一个 WPF“Box”获得了一个列表,但当我尝试添加两者时,我收到一条错误消息,提示“意外缩进”。以下是我想要实现的目标:
# 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 容器时有效,下面是列表框而不是组合框:
def setup(self):
self.lbxViews.ItemsSource = self.vm.ViewPorts
XAML如下:
<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 问题。任何帮助,将不胜感激!谢谢!
我明白了。代码很好,我在
中有一个不必要的缩进
def setup
一切顺利!
您好,我正在尝试使用 PyRevit 在 WPF 应用程序中填充 ComboBox 和 ListBox。该应用程序运行良好,其中一个 WPF“Box”获得了一个列表,但当我尝试添加两者时,我收到一条错误消息,提示“意外缩进”。以下是我想要实现的目标:
# 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 容器时有效,下面是列表框而不是组合框:
def setup(self):
self.lbxViews.ItemsSource = self.vm.ViewPorts
XAML如下:
<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 问题。任何帮助,将不胜感激!谢谢!
我明白了。代码很好,我在
中有一个不必要的缩进def setup
一切顺利!