2009-07-22 55 views
3

如何創建一個自定義控件,該控件採用UIElement的列表並根據某些邏輯呈現它們?將UIElements添加到自定義控件(WPF)

因爲它會處理的UIElement S,添加控件的最佳方式的列表將是相同的,即ListBoxComboBox

<local:SomeControl> 
    <Button Content="First"/> 
    <Label Content="Something other"/> 
</local:SomeControl> 

下面是用戶控件的XAML:

<UserControl x:Class="_2009_07_22_Wpf_Smooth_Scroller.SomeControl" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      MinHeight="100" MinWidth="100"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="*" /> 
     </Grid.RowDefinitions> 
     <Label Content="Some title"/> 

     <!-- The inner UIElement to add content to --> 
     <Canvas x:Name="innerPanel" Grid.Row="1"/> 

    </Grid> 
</UserControl> 

我如何,例如,地方i控制到位置X = 50 * 1,Y = 40 *我?

回答

4

你所描述什麼是WPF Panel

使用面板元素定位和 安排在Windows 演示基礎(WPF) 應用子對象。

也就是說,您可以子類Panel並根據您的自定義邏輯安排您的孩子。

+0

令人驚訝的簡單。剛剛嘗試過。作品。謝謝! – 2009-07-22 10:31:00

0

如果要將用戶控件添加到Panel,只需使用該面板的Children屬性。 樣品:

usercontrolObject = new MyUserControl(); 
panelobj.Children.Add(usercontrolObject); 

就是這樣!