[英]LongListSelector + Selected Item - How to read each individual properties? (WP8, Azure Database)
I have a LongListSelector, that store data from my Azure SQL Database. 我有一个LongListSelector,用于存储来自Azure SQL数据库的数据。
This is my C# code: 这是我的C#代码:
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: 这是我的XAML:
<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. 寄存器存储在我的LongListSelector中-工作正常。
Now this is my doubt: How can i read the properties for each register in my LongListSelector? 现在这是我的疑问:如何读取LongListSelector中每个寄存器的属性? For each register, i have fields such as "Id", "TypeEvent", "Hour", "Date", etc.
对于每个寄存器,我都有诸如“ Id”,“ TypeEvent”,“ Hour”,“ Date”等字段。
So, how can i read each individual value, according by the SelectedItem in LongListSelector? 那么,如何根据LongListSelector中的SelectedItem读取每个单独的值?
For example, if i wish to see in a MessageBox the ID from a Selected Item...how can i do this in code? 例如,如果我希望在MessageBox中看到Selected Item的ID,该如何在代码中进行?
I tried the following: 我尝试了以下方法:
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. * System.NullReferenceException:对象引用未设置为对象的实例。 at Project.MainPage.ContextMenuRemove_Click(Object sender, EventArgs e)*
在Project.MainPage.ContextMenuRemove_Click(Object sender,EventArgs e)*
Someone can help'me, please? 有人可以帮我吗? Thank you friends.
谢谢朋友们。
React to selection changed: 对选择做出反应:
<phone:LongListSelector Name="ListItemsToday" SelectionChanged="ListItemsToday_SelectionChanged">
<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>
with this 有了这个
private void ListItemsToday_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = ListItemsToday.SelectedItem as TodoItem;
MessageBox.Show(item.Text);
}
Or react to tap event in data template 或对数据模板中的点击事件做出反应
<phone:LongListSelector Name="ListItemsToday" >
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Tap="ListItemsToday_Tap">
<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>
like this: 像这样:
private void ListItemsToday_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
var item = (sender as FrameworkElement).DataContext as TodoItem;
MessageBox.Show(item.Text);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.