Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Databinding to an array of strings RSS

3 replies

Last post Aug 10, 2008 12:05 AM by virorum

(1)
  • Basti8

    Basti8

    Member

    66 Points

    39 Posts

    Databinding to an array of strings

    Mar 31, 2008 11:03 AM | LINK

    Hi.

    I know that it is possible to bind a Listbox to a list of objects like that:

    <ListBox x:Name="LB1">

    <ListBox.ItemTemplate>

     

    <DataTemplate>

     

    <StackPanel>

    <TextBlock Text="{Binding id}" Grid.Row="0" Grid.Column="0" Width="30" Foreground="Black" Opacity="1"></TextBlock>

    <TextBlock Text="{Binding name}"></TextBlock>

    </Grid>

    </StackPanel>

    </DataTemplate>

     

    </ListBox.ItemTemplate>

     

    </ListBox>

     

    But that's not exactly what i'm trying to do. I'm trying to bind this to an array of strings, where string [0][x] is the id and string[1][x] is name. By just substituting the List of objects by the array of strings the listbox shows no data, probably because there are no specifications which data is name and which is id, I think.

    Can you please help me?

  • swildermuth

    swildermuth

    Star

    8438 Points

    1547 Posts

    Re: Databinding to an array of strings

    Mar 31, 2008 09:45 PM | LINK

    Currently this isn't supported by DataBinding.  here's the note in the docs:

    "If you are familiar with WPF data binding, WPF supported property path techniques for retrieving indexed items in a collection, and retrieving attached property values. Silverlight currently does not provide this support. A Silverlight property path is a series of steps into an object model and only supports the "dotted" syntax."
     

    If you change the data you're using to a Dictionary (probably a better solution anyway) then you can bind to the Name or Value of each item and is likely going to be a more workable solution.  If you are getting this data via a web service, try the advanced settings to change from Array's to a better collection type.

     

    (If this has answered your question, "Mark as Answer")

    Shawn Wildermuth
    C# MVP, MCSD, Speaker and Author

    Silverlight 3 Workshop
    Miami, FL: Oct 12-14th
    Portlant, OR: Dec 2-4th
    Atlanta, GA: Dec 7-9th
    http://silverlight-tour.com
  • Basti0203

    Basti0203

    Member

    30 Points

    14 Posts

    Re: Databinding to an array of strings

    Jun 30, 2008 12:06 PM | LINK

    Hi,

     is binding to an index now supported in beta 2?
    My problem is that i get an array of objects from a webservice and i have to create a column for each item. These column must have a data template that bind its child controls to properties of the objects.

    Greets

    Sebastian Dusch

  • virorum

    virorum

    Member

    29 Points

    21 Posts

    Re: Databinding to an array of strings

    Aug 10, 2008 12:05 AM | LINK

    No, I don't think it is.

    I tried the Dictionary(Of type, type) suggestion as posted above and while this works great for getting a value from a Dictionary, it doesn't help with keeping the control up to date with changes to the value stored at that key.

    So, if you have a Text="{Binding NarrativeDictionary.ShortDesc}" this will pick out the value stored with the key ShortDesc, but if this stored value changes it doesn't then get changed in the control.

    In short, Dictionary doesn't support INotifyPropertyChanged in the way that ObservableCollection does but ObservableCollection isn't keyed.

    Can we have a keyed version of ObservableCollection? [:)]

    Also with the example Text="{Binding NarrativeDictionary.ShortDesc}" , if NarrativeDictionary is a property of a class which does support INotifyPropertyChanged and you raise the event for the NarrativeDictionary property, it doesn't rebind.

    Virorurm Media