2013-12-08 19 views
2

我有一個LongListSelector,用於存儲來自Azure SQL數據庫的數據。LongListSelector +選定的項目 - 如何閱讀每個單獨的屬性? (WP8,Azure數據庫)

這是我的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; 
    } 

這是我的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> 

寄存器存儲在我的LongListSelector - 這是工作的罰款。

現在,這是我的疑問:我如何讀取我的LongListSelector中的每個寄存器的屬性?對於每個註冊,我有諸如「Id」,「TypeEvent」,「小時」,「日期」等字段。

所以,我怎麼讀取每個單獨的值,根據LongListSelector中的SelectedItem?

例如,如果我希望在MessageBox中看到選定項目的ID ...我如何在代碼中執行此操作?

我試過如下:

var tmp1 = (TodoItem)ListItemsToday.SelectedItem; 
var tmp2 = tmp1.Id; 
MessageBox.Show(tmp2.ToString()); 

當我嘗試施放此代碼,這是我得到的錯誤:

* System.NullReferenceException:對象引用未設置爲實例一個東西。 at Project.MainPage.ContextMenuRemove_Click(Object sender,EventArgs e)*

有人可以幫忙嗎? 謝謝朋友。

回答

3

反應選擇改變:

<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> 

與此

private void ListItemsToday_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    var item = ListItemsToday.SelectedItem as TodoItem; 
    MessageBox.Show(item.Text); 
} 

還是反應在數據模板

<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> 

這樣挖掘事件:

private void ListItemsToday_Tap(object sender, System.Windows.Input.GestureEventArgs e) 
{ 
    var item = (sender as FrameworkElement).DataContext as TodoItem; 
    MessageBox.Show(item.Text); 
} 
+0

你好朋友,這正是我想要的。非常感謝!!! – user3078247

+0

Hi igrali,是否有可能獲得LLS的選定ui項目,我的意思是如果我有網格作爲數據模板,是否有可能在選擇更改事件中將網格作爲選定項目。 – Noorul

相關問題