2010-12-07 62 views
2

我已經看過了這裏的其他自定義控件創建問題以及其他在線資源,但是我失去了如何創建自定義控件不需要C#代碼隱藏文件。由於我的項目是純IronPython,我不知道如何將C#代碼與它整合在一起。在IronPython中創建自定義WPF控件:一個由標準控件構成的控件

我想要做的甚至不是那麼複雜;沒有凌亂的風格或渲染轉換或任何東西。我只是試圖創建一組現有的複合控件,以便我可以通過程序添加/刪除這些複合控件。例如,我有這樣的分組框:

正如你所看到的,它只是一個帶有文本框,並在頭一個ComboBox組框,其機身由含有一些的CheckBox一個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實現這一目標?

回答

0

我仍然覺得有一個更好的方法,所以我會留下這個問題,但我最終在IronPython中運行時添加自定義控件的方式是將XAML放在單獨的文件中,加載它與XamlReader.Load()並將返回值存儲在一個新對象中。該對象成爲控件,然後可以將其添加到GridDockPanel或您有什麼。我花了一段時間來建立心理聯繫,我可以使用XamlReader從XAML構建任何控件,而不僅僅是Window s。

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

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

XAML文件只包含與xmlns屬性建立正確的命名空間沿着上述上述GroupBox標記。

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