Results 1 to 10 of 10

Thread: How to: Create an array of controls in WPF

  1. #1

    Thread Starter
    Lively Member Nikole's Avatar
    Join Date
    Feb 2010
    Posts
    79

    How to: Create an array of controls in WPF

    Hi, i'm using Expression Blend to make an user interface. I put a text box (Text1) and when i copy the same control in the form, the new control get the name Text1_Copy. how can i create an array of controls? I mean Text1(0), text1(1), text1(2)??, please help

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How to: Create an array of controls in WPF

    Don't think you can... at least not at design time... you can load the text boxes into an array or a list(of) at runtime.

    Why do you need a control array?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Lively Member Nikole's Avatar
    Join Date
    Feb 2010
    Posts
    79

    Re: How to: Create an array of controls in WPF

    Quote Originally Posted by techgnome View Post
    Don't think you can... at least not at design time... you can load the text boxes into an array or a list(of) at runtime.

    Why do you need a control array?

    -tg

    I've started yesterday with WPF and Expression Blend. And I don't know how to use the ListView to show items like this example. Its more easy for me add controls and load images and text at runtime., like in VB6.


    I've:

    Code:
    <ListView Name="ListCovers" Margin="50,0,50,0" SelectionMode="Single" Background="Transparent">
                    <StackPanel Orientation="Vertical">
                         <Image Source="mariah.jpg"></Image>
    					 <TextBlock Text="Hola" Foreground="White" Cursor="Hand" FontSize="29.333"/>
                    </StackPanel>
    </ListView>
    How can i add more images and text from VB code?
    Code:
    ListCovers.Items.add("Text")
    , can only add text, not images
    Last edited by Nikole; Apr 2nd, 2010 at 09:38 PM.

  4. #4
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    Re: How to: Create an array of controls in WPF

    In order to display items in such a way, I would use one of these 2 methods:
    1- Using Data templates.
    2- User Controls

    Do don't necessarily have to use the ListView control to achieve that.

    Good Luck.

    P.S: When I started with WPF everything was so confusing because I never took the time to read basic concepts before starting to work. I would advice you to read some basic documentation about WPF, mainly DataTemplates, Controls, etc.....
    C# and WPF developer
    My Website:
    http://singlebits.com/

  5. #5

    Thread Starter
    Lively Member Nikole's Avatar
    Join Date
    Feb 2010
    Posts
    79

    Re: How to: Create an array of controls in WPF

    Quote Originally Posted by Pac_741 View Post
    In order to display items in such a way, I would use one of these 2 methods:
    1- Using Data templates.
    2- User Controls

    Do don't necessarily have to use the ListView control to achieve that.

    Good Luck.

    P.S: When I started with WPF everything was so confusing because I never took the time to read basic concepts before starting to work. I would advice you to read some basic documentation about WPF, mainly DataTemplates, Controls, etc.....

    Hi, i've tried for 3 days and i can't do a Listview like i want. Please, please, could you help me? could you make for me a ListView like the screenshot? and pupupate few images and text from code? i need it to understand the code and how it work , please, this is easy for u .... (excuse my english)
    Attached Images Attached Images  

  6. #6
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    Re: How to: Create an array of controls in WPF

    Programming doesn't work that way, you have to have a certain knowledge of what you are doing, which you could get by reading some stuff.

    You are right this is simple for me, but you can achieve this be reading some basic tutorials on DataTemplates, and Layouts.

    This code can get you started:

    Code:
            <!--ListView is not necessarily required here-->
            <!--Horizontal ScrollBar Visibility has to be disabled for layout to occur-->
            <ListBox x:Name="listbox"  ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                <!--Items Template-->
                <!--If you want to create a media player you will have to 
                    Investigate Data Binding-->
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <!--Width has to be set so items display properly-->
                        <StackPanel Width="50">
                            <Image Source="{Binding Path=image}"></Image>
                            <Label Content="{Binding Path=artis}"></Label>
                        </StackPanel>
                    </DataTemplate>                
                </ListBox.ItemTemplate>
                <!--Items Panel-->
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
            </ListBox>

    Please try to get to know WPF before starting to work with it, Good Luck
    C# and WPF developer
    My Website:
    http://singlebits.com/

  7. #7

    Thread Starter
    Lively Member Nikole's Avatar
    Join Date
    Feb 2010
    Posts
    79

    Re: How to: Create an array of controls in WPF

    Quote Originally Posted by Pac_741 View Post
    Programming doesn't work that way, you have to have a certain knowledge of what you are doing, which you could get by reading some stuff.

    You are right this is simple for me, but you can achieve this be reading some basic tutorials on DataTemplates, and Layouts.

    This code can get you started:

    Code:
            <!--ListView is not necessarily required here-->
            <!--Horizontal ScrollBar Visibility has to be disabled for layout to occur-->
            <ListBox x:Name="listbox"  ScrollViewer.HorizontalScrollBarVisibility="Disabled">
                <!--Items Template-->
                <!--If you want to create a media player you will have to 
                    Investigate Data Binding-->
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <!--Width has to be set so items display properly-->
                        <StackPanel Width="50">
                            <Image Source="{Binding Path=image}"></Image>
                            <Label Content="{Binding Path=artis}"></Label>
                        </StackPanel>
                    </DataTemplate>                
                </ListBox.ItemTemplate>
                <!--Items Panel-->
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
            </ListBox>

    Please try to get to know WPF before starting to work with it, Good Luck
    Gracias, probaré el código y te dire después.!

  8. #8

    Thread Starter
    Lively Member Nikole's Avatar
    Join Date
    Feb 2010
    Posts
    79

    Re: How to: Create an array of controls in WPF

    Gracias Pac_741, te molesto con una ultima pregunta: we've "image" and "artist" variable in your xaml example. how can i add a new item without databinding? c&#243;mo agrego un dato a este listview y especifico la imagen y el texto? con ListBox.items.add? pero c&#243;mo le especifico al listbox cual es la imagen? y cual es el texto?

  9. #9
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    Re: How to: Create an array of controls in WPF

    Well, if you don't want to use data binding you could create a custom control and add it directly to the listbox.
    C# and WPF developer
    My Website:
    http://singlebits.com/

  10. #10
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: How to: Create an array of controls in WPF

    As Pac said you need to figure out the basic structure of programming and wpf first. Using that picture as an example the best way to go about it would be to create a class that contains the information you need so for instance.


    Code:
    Class Album
         ArtistName
         AlbumName
         ...
    End Class
    As you can see our class only has data in it and only data applying to a single entity, so we would need to create one of these classes for each album we wanted to display for that we can put them into a ListOf(Album).

    All you need to do then is to bind or insert this list using a template like the one shown previous and you are good to go.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width