3

How can I create a custom control which takes a list of UIElements and renders them according to some logic?

Because it will handle a list of UIElements, the best way of adding controls will be the same as for, i.e. ListBox or ComboBox.

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

Here is the user control's 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>

How can I, for instance, place i-th control to location X = 50 * i, Y = 40 * i ?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
bohdan_trotsenko
  • 5,167
  • 3
  • 43
  • 70

2 Answers2

4

What you've described is a WPF Panel:

Use Panel elements to position and arrange child objects in Windows Presentation Foundation (WPF) applications.

That is, you could subclass Panel and arrange your children according to your custom logic.

Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
0

If you want to add a user control to a Panel, just use the Children property of the panel. Sample:

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

That's it!

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72