Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Gooday , I am having some C# Design trouble and I am starting to go insane trying to think of ideas so I am posting to ask for a bit of help.

I am trying to create a list of Canvas boxes that are dynamically created and put into a list that you can scroll through, they all contain the same buttons/labels ect but each with different information. I would like it to act like a ListBox. I could just use a ListBox / ListView to create what I am trying to achieve but it doesnt look as good as this. I am really unsure on where to go with this. I read I could dynamically create a canvas item and add it to a WrapPanel but I am really not sure. Its very hard to explain what I am trying to do so I do appologise for that. I have attached an image which should show you what I am trying to achieve:

http://i.imgur.com/VnAOmsI.png

Thanks in advance :)

Edit: I am using a WPF Window
Posted
Updated 7-Oct-13 2:26am
v3
Comments
Alexander Dymshyts 8-Oct-13 3:10am    
So where are you stuck at? Dynamically creating a listbox like in a link?
Silvabolt 8-Oct-13 10:01am    
Check out stackpanels, that will be your basic structure, and then add item controls in it with multiple children controls for the text/button, etc....

1 solution

I managed to solve my problem by adding a ListBox and creating a Template in the style I want for it.

I then simply added to the Listbox as you would do normally.

XAML:

XML
<ListBox x:Name="LstEvents" HorizontalAlignment="Left" Height="343" Margin="10,228,0,0" VerticalAlignment="Top" Width="524" BorderBrush="Black">
            <ListBox.Background>
                <SolidColorBrush Color="{DynamicResource WhiteColor}"/>
            </ListBox.Background>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Canvas x:Name="EventItem_Copy1" HorizontalAlignment="Left" Height="76" VerticalAlignment="Top" Width="502" Background="#99E8E8E8">
                            <Label Content="Event:" HorizontalAlignment="Left" Height="33" VerticalAlignment="Top" Width="97" FontWeight="Bold" FontSize="14" Canvas.Left="1" Canvas.Top="2"/>
                            <Label Content="Subject:" HorizontalAlignment="Left" Height="33" VerticalAlignment="Top" Width="97" FontWeight="Bold" FontSize="14" Canvas.Left="1" Canvas.Top="26"/>
                            <Label Content="Body:" HorizontalAlignment="Left" Height="33" VerticalAlignment="Top" Width="97" FontWeight="Bold" FontSize="14" Canvas.Left="1" Canvas.Top="49"/>
                            <Button Content="Select" HorizontalAlignment="Left" Height="33" VerticalAlignment="Top" Width="74" Background="#FF333333" Foreground="White" FontWeight="Normal" FontSize="14" Canvas.Left="418" Canvas.Top="24"/>
                            <Label Content="{Binding Path=Levent}" Height="25" Canvas.Left="71" Canvas.Top="4" Width="107"/>
                            <Label Content="{Binding Path=Lsubject}" Height="25" Canvas.Left="71" Canvas.Top="28" Width="107"/>
                            <Label Content="{Binding Path=Lbody}" Height="25" Canvas.Left="71" Canvas.Top="53" Width="107"/>
                        </Canvas>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>



Class:

C#
public void Button_Click_2(object sender, RoutedEventArgs e)
{

    var x = new
    {
        Levent = "EventText", //These will become variables
        Lsubject = "SubjectText",
        Lbody = "BodyText"
    };

    LstEvents.Items.Add(x);

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900