You can use ControlTemplates
and DataTriggers
to choose this, if you want more control than a StackPanel can give you. Here's a quick-n-dirty example.
Note that you can do this without using a UserControl. I'm just staying within the confines of your description.
User Control
<UserControl>
<UserControl.Resources>
<ControlTemplate x:Key="usingColumns">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ListBox Grid.Column="0" ItemsSource="{Binding DataOneItems}" />
<ListBox Grid.Column="1" ItemsSource="{Binding DataTwoItems}" />
</Grid>
</ControlTemplate>
<ControlTemplate x:Key="usingRows">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ListBox Grid.Row="0" ItemsSource="{Binding DataOneItems}" />
<ListBox Grid.Row="1" ItemsSource="{Binding DataTwoItems}" />
</Grid>
</ControlTemplate>
</UserControl.Resources>
<UserControl.Style>
<Style>
<Setter Property="UserControl.Template" Value="{StaticResource usingColumns}" />
<Style.Triggers>
<DataTrigger Binding="{Binding ShowVertically}" Value="true">
<Setter Property="UserControl.Template" Value="{StaticResource usingRows}" />
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Style>
</UserControl>
Class binding to User Control:
public class UserControlData
{
public ReadOnlyObservableCollection<DataTypeOne> DataOneItems
{
get
{
ObservableCollection<DataTypeOne> dataOneItems = new ObservableCollection<DataTypeOne>();
for (int i = 1; i <= 3; i++)
dataOneItems.Add(new DataTypeOne(i));
return new ReadOnlyObservableCollection<DataTypeOne>(dataOneItems);
}
}
public ReadOnlyObservableCollection<DataTypeTwo> DataTwoItems
{
get
{
ObservableCollection<DataTypeTwo> dataTwoItems = new ObservableCollection<DataTypeTwo>();
for (int i = 1; i <= 3; i++)
dataTwoItems.Add(new DataTypeTwo(i));
return new ReadOnlyObservableCollection<DataTypeTwo>(dataTwoItems);
}
}
public bool ShowVertically
{
get;
set;
}
}
Dummy Data Type (DataTypeOne and DataTypeTwo are identical except in class name):
public class DataTypeOne
{
private readonly int mId = 0;
public DataTypeOne(int id)
{
mId = id;
}
public int ID
{
get { return mId; }
}
public override string ToString()
{
return String.Format("I am a DataTypeOne with ID {0}", mId.ToString("N"));
}
}
The key is the ControlTemplates (one for horizontal, one for vertical) and the DataTrigger on the Style.