78490

How to dynamically add UserControl to ItemsControl?

Question:

I have a WPF UserControl which is dynamically created at runtime. Depending on user interaction, from 1-5 of these controls is created and should be added to the UI.

I have an ObservableCollection<PlayerSpec> PlayerSpecViews in the xaml.cs code-behind.

Initially I tried using a Grid and dynamically adding RowDefinitions but that wasn't working. The control would be scaled down in size.

Then I tried using an ItemsControl but the binding was not adding the Control but instead the type name.

How can I use databinding to have the control added and placed in a vertical manner? Bonus info might include vertical scroll bar if needed.

My current XAML and binding are as follows:

<ItemsControl Name="PlayerSpecItems" HorizontalContentAlignment="Stretch" ItemsSource="{Binding Source=PlayerSpecViews}"> <ItemsControl.ItemTemplate> <DataTemplate> <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="95" Width="175" Margin="1"> <local:PlayerSpec/> </StackPanel> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>

<strong>Classes XAML</strong>:

<!-- PlayerSpec --> <Border Style="{StaticResource InnerBorder}" HorizontalAlignment="Stretch"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="50"/> <ColumnDefinition Width="98"/> <ColumnDefinition Width="21"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="25"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Label Style="{StaticResource DefaultFont}" Grid.Column="0" Grid.Row="0" VerticalAlignment="Stretch" VerticalContentAlignment="Center" HorizontalAlignment="Stretch" HorizontalContentAlignment="Right" Margin="1" Content="Name"/> <TextBox Style="{StaticResource DefaultFont}" Name="PlayerName" Background="Transparent" Grid.Column="1" Grid.Row="0" Margin="1"/> <Grid Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1"> <Grid.ColumnDefinitions> <ColumnDefinition Width="25"/> <ColumnDefinition Width="25"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="25"/> <RowDefinition Height="25"/> </Grid.RowDefinitions> <Label Style="{StaticResource DefaultFont}" Grid.Column="2" Grid.Row="0" VerticalAlignment="Stretch" VerticalContentAlignment="Center" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" Content="Automaton"/> <RadioButton GroupName="PlayerTypeGroup" Name="PlayerAutomaton" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="0" Grid.IsSharedSizeScope="True" VerticalAlignment="Center"> </RadioButton> <Label Style="{StaticResource DefaultFont}" Grid.Column="2" Grid.Row="1" VerticalAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" Content="Human"/> <RadioButton GroupName="PlayerType" Name="PlayerHuman" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="1" Grid.IsSharedSizeScope="True" VerticalAlignment="Center"> </RadioButton> </Grid> </Grid> </Border>

Answer1:

Issues with your code

<ul><li>ItemsControl does not have its scrollbar so need to add an ScrollViewer</li> <li>Binding source of ItemsControl's ItemsSource is set to PlayerSpecViews which does not look normal, I expect it to be from datacontext</li> <li>Stack panel is not serving any purpose in data template</li> </ul>

so your revised code is

<ScrollViewer> <ItemsControl Name="PlayerSpecItems" HorizontalContentAlignment="Stretch" ItemsSource="{Binding PlayerSpecViews}"> <ItemsControl.ItemTemplate> <DataTemplate> <local:PlayerSpec Margin="1"/> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </ScrollViewer>

So this code should be rendering local:PlayerSpec for every item you have in your PlayerSpecViews collection subjected to correct binding

Recommend

  • What is the best way to draw a large number of lines using openGL and cocos2D?
  • How can I properly import the environment from running a subcommand in Perl?
  • Stream data into rotating log tables in BigQuery
  • In metro, what's the difference between FolderInformation and StorageFolder
  • Find generic sub-lists within a list
  • View Injection inside ItemsControl
  • Scroller-like animation on a Label in WPF
  • Nested INotifyPropertyChanged class won't work
  • Newtonsoft inline formatting for subelement while serializing
  • Why is RAM in powers of 2?
  • Visual basic auto imports namespaces
  • JQM (jQueryMobile) Problem with AJAX content and focus()
  • wrong item changes in recyclerview
  • How to expand parent div to account for child's overflow?
  • WPF Listbox commands
  • C# List of Panels
  • New Firebase failed: First argument must be a valid firebase URL and the path can't contain “.”
  • JSON encode and decode on PHP
  • Building Qt project for C++11 standard
  • Consuming a WCF service in a Java Client using wsHttpBinding
  • Reading a file into a multidimensional array
  • Object and struct member access and address offset calculation
  • Force show.bind execution
  • Hardware Accelerated Image Scaling in windows using C++
  • Magento Fatal error: Maximum execution error solution, on WAMP
  • How to rebase a series of branches?
  • Get one-time binding to work for ng-if
  • Nant, Vault & Windows Integrated Authentication
  • Align navbar back button on right side
  • Bug in WPF DataGrid
  • Window Size for Mac application
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • jQuery tmpl and DataLink beta
  • How can I estimate amount of memory left with calling System.gc()?
  • QuartzCore.framework for Mono Develop
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Buffer size for converting unsigned long to string
  • Hits per day in Google Big Query
  • How to get Windows thread pool to call class member function?
  • How to push additional view controllers onto NavigationController but keep the TabBar?