×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Surat Thani
    Posts
    260

    Styling ListBox items with hardcoded data

    Hello,

    I'd like to create several similar-looking ListBoxes as pages of a Pivot. The content of all the lists is predefined and doesn't change, and all the items of every list look the same, each having just two lines of text. If possible, the texts should be defined in the same XAML file as the ListBox they're in. Is there any way to use ListBox.ItemTemplate or something similar to specify how the items look (margins, text style etc) once, or at least once per list, so the stuff doesn't need to be copy-pasted for every item separately (ugly!)?

    Item/DataTemplates work with binding, but it seems like a lot of work for just a few static items per list. If going for this alternative, what would be the best way to include the texts in the project so that they can be both loaded and localized easily?

    Edit: Clarified that the content of the lists doesn't change.
    Last edited by TongMuan; 2014-01-09 at 17:45.

  2. #2
    Super Contributor
    Join Date
    Feb 2013
    Location
    Dublin, Ireland
    Posts
    578

    Re: Styling ListBox items with hardcoded data

    It you want to display the same list of data in several pivots, then I'd do the following...

    1. Make sure your data is in a ViewModel (VM) which can be bound with the View (ie. the page).
    2. Use a LongListSelector (replaces ListBox) in each PivotItem.

    The cut down on duplication there are few more things you can do...

    1. In the VM, load the data from a file (eg. CSV file) as opposed to it being hard code. This will make it easier to localise later.
    2. When you create an ItemTemplate for a LongListSelector you can place it in another file (eg. app.xaml) and reference it in ALL your LongListSelectors

    Does that make sense?

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Surat Thani
    Posts
    260

    Re: Styling ListBox items with hardcoded data

    Thanks a lot for the quick reply! Thought about this a bit more and, as I actually have just one Pivot with 3 different lists of 5 items each, having to write all the code for data loading and binding might be a bit of an overkill.

    Finally decided to implement an UserControl, which takes takes the item title and subtitle as attributes, so the texts for ListBox items can be defined in the XAML like this:

    Code:
    <ListBox.Items>
      <ListBoxItem>
        <local:TwoTexts Title="Item Title" SubTitle="Item SubTitle" />
      </ListBoxItem>
      <ListBoxItem>
        <local:TwoTexts Title="Item Title 2" SubTitle="Item SubTitle 2" />
      </ListBoxItem>
    </ListBox.Items>
    The styling stuff (which would have been in DataTemplate in case of binding) is done in the UserControl's XAML file.

  4. #4
    Super Contributor
    Join Date
    Feb 2013
    Location
    Dublin, Ireland
    Posts
    578

    Re: Styling ListBox items with hardcoded data

    Seems fine - obviously you know your own requirements, so the CSV option was just one of many ways of doing it.

    If you're hard coding strings into the app but want to localise it later, this is a great guide...

    http://developer.nokia.com/Community...al_App_Toolkit

Similar Threads

  1. Replies: 11
    Last Post: 2013-10-04, 12:29
  2. How to add new items in ListBox?
    By ambkps in forum Symbian
    Replies: 26
    Last Post: 2010-03-21, 00:08
  3. Items in Listbox do not appear???
    By Orange_LEE in forum Symbian User Interface
    Replies: 9
    Last Post: 2008-04-02, 12:12
  4. How to associate data with listbox items
    By vnleeds in forum Symbian User Interface
    Replies: 2
    Last Post: 2007-08-13, 19:01
  5. items of listbox
    By letiziad in forum Symbian
    Replies: 4
    Last Post: 2006-11-10, 14:12

Posting Permissions

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