在 IronPython 中创建自定义 WPF 控件:由标准控件组成的控件
我已经查看了此处的其他自定义控件创建问题以及其他在线资源,但我不知道如何创建不需要 C# 代码隐藏文件的自定义控件。由于我的项目是纯 IronPython,我不知道如何将后面的 C# 代码与其集成。
我想做的事情并没有那么复杂;没有凌乱的样式或渲染转换或任何东西。我只是想创建一个现有的一堆复合控件,以便我可以按程序添加/删除这些复合控件。例如,我有这个 GroupBox:
如您所见,它只是一个 GroupBox标题中包含一个文本框和一个组合框,其主体由包含一些复选框的 UniformGrid 填充。这是 XAML:
<GroupBox Name="DO1">
<GroupBox.Header>
<DockPanel>
<TextBlock VerticalAlignment="Center">DO1</TextBlock>
<ComboBox Name="DO1DeviceList" Margin="3 0 0 0" ItemsSource="{Binding DOtable}">
</ComboBox>
</DockPanel>
</GroupBox.Header>
<UniformGrid Rows="1">
<StackPanel>
<Label Content="3W1L" HorizontalAlignment="Center" VerticalAlignment="Center" />
<CheckBox Name="DO1o1" IsChecked="{Binding o1}" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Label Content="1" HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
<StackPanel>
<Label Content="3W3V" HorizontalAlignment="Center" VerticalAlignment="Center" />
<CheckBox Name="DO1o2" IsChecked="{Binding o2}" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Label Content="2" HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
<StackPanel>
<Label Content="Ni" HorizontalAlignment="Center" VerticalAlignment="Center" />
<CheckBox Name="DO1o3" IsChecked="{Binding o3}" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Label Content="3" HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
<StackPanel>
<Label Content="2W1" HorizontalAlignment="Center" VerticalAlignment="Center" />
<CheckBox Name="DO1o4" IsChecked="{Binding o4}" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Label Content="4" HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
<StackPanel>
<Label Content="Vac2" HorizontalAlignment="Center" VerticalAlignment="Center" />
<CheckBox Name="DO1o5" IsChecked="{Binding o5}" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Label Content="5" HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
<StackPanel>
<Label Content="3W4V" HorizontalAlignment="Center" VerticalAlignment="Center" />
<CheckBox Name="DO1o6" IsChecked="{Binding o6}" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Label Content="6" HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
<StackPanel>
<Label Content="Cu" HorizontalAlignment="Center" VerticalAlignment="Center" />
<CheckBox Name="DO1o7" IsChecked="{Binding o7}" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Label Content="7" HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
<StackPanel>
<Label Content="3W2L" HorizontalAlignment="Center" VerticalAlignment="Center" />
<CheckBox Name="DO1o8" IsChecked="{Binding o8}" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Label Content="8" HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
</UniformGrid>
</GroupBox>
我读过有关控件合成的内容,即将一堆控件分组为一个控件。我想让整个 GroupBox 成为一个可以按程序添加或删除的单个控件。我如何使用 IronPython 来完成此任务?
I've looked at the other custom control creation questions on here, as well as other online resources, but I'm lost as to how to create a custom control that doesn't require a C# code-behind file. Since my project is pure IronPython, I don't know how I would integrate the C# code behind with it.
What I'm trying to do isn't even that complicated; no messy styling or render transformations or anything. I'm merely trying to create a composite control of a bunch of existing ones I have, so that I can add/remove these composite controls procedurally. For example, I have this GroupBox:
As you can see, it's just a GroupBox with a TextBox and a ComboBox in the Header, with its body populated by a UniformGrid containing some CheckBoxes. Here is the XAML:
<GroupBox Name="DO1">
<GroupBox.Header>
<DockPanel>
<TextBlock VerticalAlignment="Center">DO1</TextBlock>
<ComboBox Name="DO1DeviceList" Margin="3 0 0 0" ItemsSource="{Binding DOtable}">
</ComboBox>
</DockPanel>
</GroupBox.Header>
<UniformGrid Rows="1">
<StackPanel>
<Label Content="3W1L" HorizontalAlignment="Center" VerticalAlignment="Center" />
<CheckBox Name="DO1o1" IsChecked="{Binding o1}" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Label Content="1" HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
<StackPanel>
<Label Content="3W3V" HorizontalAlignment="Center" VerticalAlignment="Center" />
<CheckBox Name="DO1o2" IsChecked="{Binding o2}" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Label Content="2" HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
<StackPanel>
<Label Content="Ni" HorizontalAlignment="Center" VerticalAlignment="Center" />
<CheckBox Name="DO1o3" IsChecked="{Binding o3}" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Label Content="3" HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
<StackPanel>
<Label Content="2W1" HorizontalAlignment="Center" VerticalAlignment="Center" />
<CheckBox Name="DO1o4" IsChecked="{Binding o4}" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Label Content="4" HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
<StackPanel>
<Label Content="Vac2" HorizontalAlignment="Center" VerticalAlignment="Center" />
<CheckBox Name="DO1o5" IsChecked="{Binding o5}" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Label Content="5" HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
<StackPanel>
<Label Content="3W4V" HorizontalAlignment="Center" VerticalAlignment="Center" />
<CheckBox Name="DO1o6" IsChecked="{Binding o6}" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Label Content="6" HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
<StackPanel>
<Label Content="Cu" HorizontalAlignment="Center" VerticalAlignment="Center" />
<CheckBox Name="DO1o7" IsChecked="{Binding o7}" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Label Content="7" HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
<StackPanel>
<Label Content="3W2L" HorizontalAlignment="Center" VerticalAlignment="Center" />
<CheckBox Name="DO1o8" IsChecked="{Binding o8}" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Label Content="8" HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
</UniformGrid>
</GroupBox>
I've read about control compositing, grouping a bunch of controls into a single control. I'd like to make this whole GroupBox a single control I can add or remove procedurally. How can I accomplish this with IronPython?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我仍然觉得有更好的方法,所以我将保留这个问题,但我最终在 IronPython 中添加自定义控件的运行时的方法是简单地将 XAML 放在一个单独的文件中,使用
XamlReader.Load()
并将返回值存储在新对象中。该对象成为控件,然后可以将其添加到Grid
、DockPanel
或您拥有的其他控件中。我花了一段时间才意识到我可以使用XamlReader
从 XAML 构造任何控件,而不仅仅是Window
。XAML 文件仅包含上述
GroupBox
标记以及用于建立正确命名空间的xmlns
属性。I still feel like there's a better way, so I'll leave this question open, but the way I ended up doing run-time addition of custom controls in IronPython is by simply placing the XAML in a seperate file, loading it with
XamlReader.Load()
and storing the return value in a new object. That object becomes the control, which can then be added to aGrid
,DockPanel
, or what have you. It took me a while to make the mental connection that I could useXamlReader
to construct any control from XAML, not justWindow
s.The XAML file simply contains the above aforementioned
GroupBox
markup along with anxmlns
attribute to establish the right namespace.