×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    4

    Creating a listbox in windows phone 7

    Hi

    I want to create a listbox(drop down) with list items in it.When user wants to select the item from listbox he should be able to scroll down or scroll up within the listbox.

    SO i have the following xaml for it..
    <ListBox Height="100" HorizontalAlignment="Left" Margin="101,63,0,0" Name="listBox1" VerticalAlignment="Top" Width="349">
    <ListBox.ItemTemplate>
    <DataTemplate>
    <StackPanel Orientation="Vertical">
    <TextBlock Text=" {Binding dtDate}">

    </TextBlock>
    </StackPanel>
    </DataTemplate>
    </ListBox.ItemTemplate>
    </ListBox>

    And i have the following C# code.

    public MainPage()
    {
    InitializeComponent();
    FillListBox();

    }

    private void FillListBox()
    {

    List<string> dtday = new List<string>();
    Console.WriteLine("\nCapacity: {0}", dtday.Capacity);
    dtday.Add("1");
    dtday.Add("2");
    dtday.Add("3");
    dtday.Add("4");
    dtday.Add("5");

    listBox1.ItemsSource = dtday;
    }

    But when i ran the program it always shows the text as {Binding dtDate}" not the values (1,2,3,4) i have written in the C# code.

    Any help would be greatly appreciated..

  2. #2
    Nokia Developer Champion
    Join Date
    Feb 2012
    Location
    Bilbao
    Posts
    53

    Re: Creating a listbox in windows phone 7

    I dont know if this is a mistake when pasting code here, but in the <TextBlock Text=" {Binding dtDate}"> you have an space between " and {, if this is not a copy & paste mistake, please remove it for the binding to work correctly.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    82

    Re: Creating a listbox in windows phone 7

    From the c# code in FillListBox, the array dtday is assigned to the listbox's ItemsSource, so for each item in the listbox, it will use the array's item's property to make binding.
    And in the xaml code, the binding is Text="{Binding dtDate}", but the dtDate is not a property which can be bound.
    You can change the binding to Text="{Binding Path=Length}" to have a try. It will make the listbox show "1" for each item.
    And to make the listbox show the value in the array, please just change the binding to Text="{Binding}" to have a try.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    4

    Re: Creating a listbox in windows phone 7

    Hi

    Thanks for your reply.I have changed the xaml code to below.Though it displays the items now, it tries displays all of them within the list box area.So when i have 15 items, it shows only first 4 items.
    I want this listbox to display the first item by default and when user clicks on that listbox, it should show me all the values with an option to scroll down or up to select a particular item.

    <ListBox SelectionMode="Single" Height="100" HorizontalAlignment="Left" Margin="43,316,0,0" Name="listBox1" ItemsSource="{Binding}" VerticalAlignment="Top" Width="78">
    <ListBox.ItemTemplate>
    <DataTemplate>
    <StackPanel Orientation="Vertical">
    <TextBlock Text="{Binding}" >

    </TextBlock>
    </StackPanel>
    </DataTemplate>
    </ListBox.ItemTemplate>

    </ListBox>

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    82

    Re: Creating a listbox in windows phone 7

    If you need to dynamically display data on the UI, you need to dynamically update your data.
    It's better you update your code by using MVVM model so as you will only need to update your data in ViewModel dynamically.

  6. #6
    Nokia Developer Champion
    Join Date
    Feb 2012
    Location
    Bilbao
    Posts
    53

    Re: Creating a listbox in windows phone 7

    Hi!

    What you need is not a listbox. A Listbox display all items and allows the user to pick one, what you need is a ListPicker, a control included with the Silverlight for Windows Phone Toolkit (silverlight.codeplex.com) it behaves like the background color picker in the phone settings, display only the selected item and when you click on it, open to show all available values.

  7. #7
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    India
    Posts
    1,173

    Re: Creating a listbox in windows phone 7

    But when i ran the program it always shows the text as {Binding dtDate}" not the values (1,2,3,4)
    It should not show you the strings (1,2,3,4) as you are using following code:
    Code:
    <TextBlock Text=" {Binding dtDate}">
    I hope now what you have to recheck.
    Also, agreed with post # 6.

Similar Threads

  1. Drill down listbox with several detail views for windows phone
    By livinglifehappy in forum Windows Phone General
    Replies: 2
    Last Post: 2011-11-08, 01:55
  2. Creating a Windows phone interface GUI
    By PeterStroud in forum General Development Questions
    Replies: 6
    Last Post: 2008-03-24, 03:51
  3. Creating a drive in Windows of my memory in my cellphone?
    By Axe air in forum General Development Questions
    Replies: 7
    Last Post: 2006-06-02, 11:27
  4. Creating ListBox
    By amitaggarwal in forum Symbian
    Replies: 6
    Last Post: 2004-08-24, 03:48
  5. Replies: 1
    Last Post: 2002-07-16, 06:44

Posting Permissions

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