0

Hello I'm trying to do a foreach loop that will create an itemscontrol with an image, a textblock and 2 buttons as child of another itemscontrol. I want to have the textblock.text of the textblock as the string in the loop.

example

Xaml:

<Grid>
    <ItemsControl x:Name="MainControl" Margin="0,175,0,22">
        <ItemsControl Margin="10,0,0,0" Style="{StaticResource HorizontalItemsControl}">
            <Border Background="Black" BorderBrush="#FF1EF3F3" BorderThickness="2">
                <Image  Height="50" Width="50" Source="{StaticResource SoulsRebornFlame}"/>
            </Border>
            <TextBlock VerticalAlignment="Center" x:Name="PendingUsername" FontSize="18" Text="Username" FontFamily="Segoe UI Black" Margin="15,0,0,0" Foreground="White"/>

            <Button Margin="15,0,0,0" Style="{StaticResource MyButtonStyle}">
                <TextBlock VerticalAlignment="Center" FontSize="18" Text="V" FontFamily="Segoe UI Black" Foreground="Lime"/>
            </Button>
            <Button Margin="15,0,0,0" Style="{StaticResource MyButtonStyle}">
                <TextBlock VerticalAlignment="Center" FontSize="18" Text="X" FontFamily="Segoe UI Black" Foreground="Red"/>
            </Button>
        </ItemsControl>
    </ItemsControl>
</Grid>

c#

foreach(string senderusername in dataCollection)
        {
            //Create new itemscontrol with the childs and set the textblock.text equal to snederusername.
        }

So i want to programatically make the xaml objects foreach string int the list dataColletion.

How do i do this ? Thanks in advance.

Community
  • 1
  • 1
Ruben Versavel
  • 161
  • 2
  • 12
  • I believe what you are looking for are `DataTemplates`. Consider looking at [MSDN](https://msdn.microsoft.com/en-us/library/system.windows.datatemplate(v=vs.110).aspx) for an example. – Sudsy1002 Feb 14 '18 at 16:03
  • will do , Thanks – Ruben Versavel Feb 14 '18 at 16:03
  • Why don't you set the ItemsSource property of the ItemsControl to your dataCollection and bind to the properties? – mm8 Feb 14 '18 at 16:04
  • 1
    A nested ItemsControl in an ItemsControl doesn't make sense here. Assign your dataCollection (e.g. by data binding) to an ItemsControl's ItemsSource property. Then assign a DataTemplate to the ItemsControl's ItemTemplate property (by declaring it in XAML). In the DataTemplate, create UI elements that are bound to properties of your data item class, i.e. the element type of dataCollection. Start reading here: [Data Templating Overview](https://learn.microsoft.com/en-us/dotnet/framework/wpf/data/data-templating-overview) – Clemens Feb 14 '18 at 16:04

0 Answers0