在 IronPython 中创建自定义 WPF 控件:由标准控件组成的控件

发布于 2024-10-07 00:27:14 字数 3908 浏览 3 评论 0原文

我已经查看了此处的其他自定义控件创建问题以及其他在线资源,但我不知道如何创建不需要 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别低头,皇冠会掉 2024-10-14 00:27:14

我仍然觉得有更好的方法,所以我将保留这个问题,但我最终在 IronPython 中添加自定义控件的运行时的方法是简单地将 XAML 放在一个单独的文件中,使用 XamlReader.Load() 并将返回值存储在新对象中。该对象成为控件,然后可以将其添加到 GridDockPanel 或您拥有的其他控件中。我花了一段时间才意识到我可以使用 XamlReader 从 XAML 构造任何控件,而不仅仅是 Window

with File.OpenRead("MyControl.xaml") as xamlfile:
    newcontrol = System.Windows.Markup.XamlReader.Load(xamlfile)

ui.FindName("MyDockPanel").Children.Add(newcontrol)

XAML 文件仅包含上述 GroupBox 标记以及用于建立正确命名空间的 xmlns 属性。

<GroupBox Name="DO1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <GroupBox.Header>
    ...

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 a Grid, DockPanel, or what have you. It took me a while to make the mental connection that I could use XamlReader to construct any control from XAML, not just Windows.

with File.OpenRead("MyControl.xaml") as xamlfile:
    newcontrol = System.Windows.Markup.XamlReader.Load(xamlfile)

ui.FindName("MyDockPanel").Children.Add(newcontrol)

The XAML file simply contains the above aforementioned GroupBox markup along with an xmlns attribute to establish the right namespace.

<GroupBox Name="DO1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <GroupBox.Header>
    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文