LongListSelector + Selected Item - How to read each individual properties? (WP8, Azure Database)

rsantiago

New member
Dec 8, 2013
1
0
0
Visit site
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;
    }

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>

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());

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.
 

Bulow

New member
Dec 2, 2013
8
0
0
Visit site
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()
 

Members online

Forum statistics

Threads
323,300
Messages
2,243,598
Members
428,055
Latest member
DrPendragon