1. rsantiago's Avatar
    Hello friends, i'm a new developer in Windows Phone, and i have a LongListSelector, that store data from my Azure SQL Database.

    This is my C# code:
    [CODE] private async void RefreshTodoItemsToday()
    {
    try
    {
    coll = await todoTable
    .Where(todoItem => todoItem.TpEvt == "today")
    .ToCollectionAsync();
    }
    catch (MobileServiceInvalidOperationException e)
    {
    MessageBox.Show(e.Message, "Error loading items", MessageBoxButton.OK);
    }

    ListItemstoday.ItemsSource = coll;
    }[/CODE]

    And this is my XAML:

    [CODE]<phone:LongListSelector Name="ListItemsToday">
    <phone:LongListSelector.ItemTemplate>
    <DataTemplate>
    <StackPanel>

    <TextBlock Name="TxtEvt" Text="{Binding Text}" TextWrapping="Wrap" Foreground="Gray" TextAlignment="Center" FontSize="30" Padding="30">
    </TextBlock>

    <Line X1="0" Y1="10" X2="240" Y2="10" Stroke="SkyBlue" HorizontalAlignment="Center" VerticalAlignment="Center" Height="21"/>
    </StackPanel>
    </DataTemplate>
    </phone:LongListSelector.ItemTemplate>
    </phone:LongListSelector>[/CODE]

    The registers are stored in my LongListSelector - it is working fine.

    Now this is my doubt: How can i read the properties for each register in my LongListSelector? For each register, i have fields such as "Id", "TypeEvent", "Hour", "Date", etc.

    So, how can i read each individual value, according by the SelectedItem in LongListSelector?

    For example, if i wish to see in a MessageBox the ID from a Selected Item...how can i do this in code?

    I tried the following:
    [CODE]var tmp1 = (TodoItem)ListItemsToday.SelectedItem;
    var tmp2 = tmp1.Id;
    MessageBox.Show(tmp2.ToString());[/CODE]

    When i try to cast this code, this is the error i got:

    *System.NullReferenceException: Object reference not set to an instance of an object. at Project.MainPage.ContextMenuRemove_Click(Object sender, EventArgs e)*

    Someone can help'me, please? Thank you friends.
    12-08-2013 10:37 AM
  2. nemecek_f's Avatar
    Why use another var variable? MessageBox.Show(tmp1.Id.ToString()); should work fine.
    12-08-2013 02:43 PM
  3. Bulow's Avatar
    Without trying anything in code, I would say that it's because no item is selected. You should only use list.SelectedItem, when you know that an item has actually been selected (eg. in the eventhandler of list.SelectionChanged), or at least ensure that it isn't null first.

    Instead, you could let each TodoItem have a unique id that you bind to the Tag of the ContextMenuRemove buttons (if you placed them in the ItemTemplate of your LongListSelector). Then you can use linq to get the appropriate TodoItem in ContextMenuRemove_Click()
    12-14-2013 01:08 PM

Similar Threads

  1. Confused about how to access the 41 mpixel images
    By scgf in forum Nokia Lumia 1020
    Replies: 10
    Last Post: 12-10-2013, 02:32 PM
  2. How to enable EAP SIM, is it available?
    By jlanzac in forum Windows Phone 8
    Replies: 0
    Last Post: 12-08-2013, 05:52 AM
  3. Plex WP8 Update
    By lucas.scott in forum Applications
    Replies: 1
    Last Post: 12-08-2013, 04:04 AM
LINK TO POST COPIED TO CLIPBOARD