使用嵌套网格动态生成的列表框

时间:2014-02-04 20:00:23

标签: c# windows-phone-8 listbox grid

我有这个c#代码,我从xml文件中读取项目并在ListBox中排列图像和标题。我还在列表框中添加了一个SelectionChanged事件处理程序来处理选择。但是,当我单击ListBox中的任何行时,publicationsList_SelectionChanged事件处理程序告诉我正在选择网格控件而不是列表框控件。这不允许我使用listbox的selectedItem属性获取并使用选定的标题和图像导航到下一页。请帮忙。

// Deserialize if download succeeds
XmlSerializer serializer = new XmlSerializer(typeof(Publications));
XDocument document = XDocument.Parse(e.Result);
// get all the employees
Publications publications = (Publications)serializer.Deserialize(document.CreateReader());

Grid grid1 = new Grid();
// bind data to ListBox
ListBox listBox = new ListBox();

foreach (Publication pub in publications.itemsPublications)
{
   Grid grid = new Grid();
   Image itemImage = new Image();
   BitmapImage BitImg = new BitmapImage(new Uri(pub.imageurl, UriKind.Absolute)); 
   itemImage.Source = BitImg;

   itemImage.Width= 97;
   itemImage.Height = 125;

   Grid.SetRow(itemImage, 0);
   Grid.SetColumn(itemImage, 0);
   itemImage.SetValue(Grid.ColumnProperty, 0);

   ColumnDefinition columnDefinition1 = new ColumnDefinition();
   ColumnDefinition columnDefinition2 = new ColumnDefinition();
   columnDefinition1.Width = new GridLength(100);
   columnDefinition2.Width = new GridLength(250);
   grid.ColumnDefinitions.Add(columnDefinition1);
   grid.ColumnDefinitions.Add(columnDefinition2);
   grid.Children.Add(itemImage);
   listBox.Items.Add(grid);

   StackPanel stackPanel1 = new StackPanel();
   TextBlock titleBlock = new TextBlock();
   titleBlock.Text = pub.title;
   stackPanel1.Margin = new Thickness(0, 15, 0, 0);
   stackPanel1.Height = 60;
   stackPanel1.Children.Add(titleBlock);
   stackPanel1.SetValue(Grid.ColumnProperty, 1);
   grid.Children.Add(stackPanel1);
}

listBox.SelectionChanged += publicationsList_SelectionChanged;
PivotItem pvt = e.UserState as PivotItem;
grid1.Children.Add(listBox);
pvt.Content = grid1;


private void publicationsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
        var app = App.Current as App;
        // Get the currently selected item in the ListBox. 
        //app.selectedPublication = (Publication)(sender as Grid);
        MessageBox.Show(e.AddedItems[0].ToString());
        this.NavigationService.Navigate(new Uri("/PublicationPage.xaml", UriKind.Relative));
}

enter link description here 出版物类

public class Publication
{
    [XmlElement("title")]
    public string title { get; set; }

    [XmlElement("imageurl")]
    public string imageurl { get; set; }

    [XmlElement("price")]
    public string price { get; set; }

    [XmlElement("author")]
    public string author { get; set; }

    [XmlElement("itempages")]
    public string itempages { get; set; }

    [XmlElement("alias")]
    public string alias { get; set; }
}

出版物类

[XmlRoot("root")]
public class Publications
{
    [XmlArray("publications")]
    [XmlArrayItem("publication")]
    //        public ObservableCollection<Publication> Collection { get; set; }
    public Publication[] itemsPublications {get; set;}
}

[1]: http://www.thegatekeepers.ng/screenshot1.png

1 个答案:

答案 0 :(得分:0)

当你说 publicationsListSelectionChanged事件处理程序告诉我正在选择网格控件而不是列表框控件时,我不知道你的意思,因为这完全没有意义。要访问ListBox处理程序中的SelectionChanged,您只需将sender输入参数强制转换为ListBox即可。但是,您甚至不需要访问ListBox,因为您可以直接从SelectionChangedEventArgs对象访问所选项目:

private void publicationsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // Get the currently selected item from the ListBox
    ListBox listBox = (ListBox)sender;
    Publication selectedItem = (Publication)listBox.SelectedItem;
    // Or you can just do this without the ListBox reference
    selectedItem = (Publication)e.AddedItems[0]);
    ...
}

更新&gt;&gt;&gt;

我无法理解您的listBox.SelectedItem财产为何返回Grid ...虽然我看不到明显的问题,但必须代码中的某个地方有错误。如果您现在将应用程序编写为WPF应用程序而不是WinForm应用程序,那么您可能不会遇到这些问题。您应该阅读MSDN上的Introduction to WPF页面,了解如何正确执行操作。

但是,如果您的listBox.SelectedItem属性 返回Grid,那么您可以尝试一些事情。首先,查看您的对象是否已设置为Grid.DataContext

private void publicationsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ListBox listBox = (ListBox)sender;
    Grid selectedGrid = (Grid)listBox.SelectedItem;
    Publication selectedItem = (Publication)selectedGrid.DataContext;
    ...
}

如果仍然无法获取您的数据对象,请在第三行放置一个断点,并调查Grid内的数据项以及数据项所在的位置。

另外,你也尝试过其他方法吗?:

private void publicationsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ListBox listBox = (ListBox)sender;
    Publication selectedItem = (Publication)e.AddedItems[0]);
    ...
}