1

I'm trying to create a User Control that accepts a collection of items directly into XAML (without binding the DataContext), like the ComboBox:

    <ComboBox>
        <ComboBoxItem Content="test"/>
        <ComboBoxItem Content="test2"/>
        <ComboBoxItem Content="test3"/>
    </ComboBox>

Also, in the class of my control, I want to receive a List of the items that are inputed inside the UC on XAML. Is this possible?

** Edit **

I need to receive more than one list, say, three lists:

    <MyControl>
        <MyControl.FirstCollection>
            <MyControlItem Content="test"/>
            <MyControlItem Content="test2"/>
            <MyControlItem Content="test3"/>
        </MyControl.FirstCollection>
        <MyControl.SecondCollection>
            <MyControlItem Content="test4"/>
            <MyControlItem Content="test5"/>
            <MyControlItem Content="test6"/>
        </MyControl.SecondCollection>
        <MyControl.LastCollection>
            <MyControlItem Content="test7"/>
            <MyControlItem Content="test8"/>
            <MyControlItem Content="test9"/>
        </MyControl.LastCollection>
    </MyControl>

How can I receive three lists?

Guilherme
  • 5,143
  • 5
  • 39
  • 60
  • Duplicate of [Adding children to UserControl](http://stackoverflow.com/questions/9094486/adding-children-to-usercontrol). I'm using the solution from the accepted answer and it works perfectly. – Federico Berasategui Aug 09 '13 at 02:11
  • Good solution, but I don't want to use a container to make my items visible, I only want to get the Children controls and put it into a List or Collection. – Guilherme Aug 09 '13 at 02:28
  • I'm not sure I understand your previous comment. What do you mean by "use a container"? – Federico Berasategui Aug 09 '13 at 02:30
  • Sorry, I was unclear. I mean container something like a stackpanel, grid, contentcontrol, canvas or anything else, like the stackpanel used into the answer here: http://stackoverflow.com/questions/9094486/adding-children-to-usercontrol – Guilherme Aug 09 '13 at 02:35

1 Answers1

1

I don't want to use a container to make my items visible, I only want to get the Children controls and put it into a List or Collection

If they are not visual elements you can just create Lists on your user control

Example: (using just FrameworkElement lists)

UserControl:

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    public ObservableCollection<FrameworkElement> FirstCollection
    {
        get { return (ObservableCollection<FrameworkElement>)GetValue(FirstCollectionProperty); }
        set { SetValue(FirstCollectionProperty, value); }
    }

    // Using a DependencyProperty as the backing store for FirstCollection.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty FirstCollectionProperty =
        DependencyProperty.Register("FirstCollection", typeof(ObservableCollection<FrameworkElement>), typeof(UserControl1), new PropertyMetadata(new ObservableCollection<FrameworkElement>()));

    public ObservableCollection<FrameworkElement> SecondCollection
    {
        get { return (ObservableCollection<FrameworkElement>)GetValue(SecondCollectionProperty); }
        set { SetValue(SecondCollectionProperty, value); }
    }

    // Using a DependencyProperty as the backing store for SecondCollection.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SecondCollectionProperty =
        DependencyProperty.Register("SecondCollection", typeof(ObservableCollection<FrameworkElement>), typeof(UserControl1), new PropertyMetadata(new ObservableCollection<FrameworkElement>()));

    public ObservableCollection<FrameworkElement> LastCollection
    {
        get { return (ObservableCollection<FrameworkElement>)GetValue(LastCollectionProperty); }
        set { SetValue(LastCollectionProperty, value); }
    }

    // Using a DependencyProperty as the backing store for LastCollection.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty LastCollectionProperty =
        DependencyProperty.Register("LastCollection", typeof(ObservableCollection<FrameworkElement>), typeof(UserControl1), new PropertyMetadata(new ObservableCollection<FrameworkElement>()));

}

Usage:

<Window x:Class="WpfApplication8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication8"
        Title="MainWindow" Height="350" Width="525" Name="UI">
    <Grid>
        <local:UserControl1 >
            <local:UserControl1.FirstCollection>
                <Label />
            </local:UserControl1.FirstCollection>
            <local:UserControl1.SecondCollection>
                <Label />
            </local:UserControl1.SecondCollection>
            <local:UserControl1.LastCollection>
                <Label />
            </local:UserControl1.LastCollection>
        </local:UserControl1>
    </Grid>
</Window>
sa_ddam213
  • 42,848
  • 7
  • 101
  • 110