1

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

我的需要是我想在单击特定行时获取每一行的标题和副标题。有没有办法在不从提供文本的数组中获取文本的情况下获取文本。

4

1 回答 1

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

主页.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

公共 ObservableCollection MyAccounts { 获取;放; }

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;
        }
}
于 2012-05-01T19:17:10.910 回答