从列表视图中获取文本

时间:2012-05-01 18:32:57

标签: windows windows-phone-7

我按照此链接中的教程制作了自定义列表视图 http://www.coderzheaven.com/2012/03/23/how-to-create-a-custom-listbox-in-windows-phone-7/

我需要的是在点击特定行时我想获得每行的标题和副标题。是否有任何方法可以在不从提供文本的数组中获取文本。

1 个答案:

答案 0 :(得分:1)

public class Account
{
   public string Title { get; set; }
   public string SubTitle { get; set; }
   public string ImageUrl { get; set; }
}

MainPage.xaml

<ListBox Margin="12,75,12,0" Name="L1"  SelectionChanged="ListBox_SelectionChanged">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Margin="0,0,0,17" Width="432" Height="78">
                                <TextBlock  TextWrapping="Wrap" Text="{Binding Title}" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                                <TextBlock  TextWrapping="Wrap" Text="{Binding SubTitle}" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                            </StackPanel>
                        </DataTemplate>
</ListBox.ItemTemplate>
                </ListBox>

MainPage.xaml.cs

public ObservableCollection MyAccounts {get;组; }

public MainPage()
{
  Loaded += MainPageLoaded;
}
// Load data for the ViewModel Items
        private void MainPageLoaded(object sender, RoutedEventArgs e)
        {
                MyAccounts = GenerateAccounts(); //populate your list
                L1.ItemsSource = MyAccounts;

        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
               var lb = (ListBox)sender;
               if (lb.SelectedIndex == -1)return;
               var account = (Account)sender.SelectedItem;
              //get properties from Account npw.
              lb.SelectedIndex = -1;
        }
}