Creating Custom WPF Controls in IronPython: a control made up out of standard controls
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?
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 Window
s.
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>
...
精彩评论