【问题标题】:Dynamically generated Listbox with nested Grid带有嵌套网格的动态生成的列表框
【发布时间】:2014-02-28 23:51:45
【问题描述】:

我有这个 c# 代码,我从 xml 文件中读取项目并将图像和标题排列在 ListBox 中。我还在列表框中添加了一个 SelectionChanged 事件处理程序来处理选择。但是,当我单击 ListBox 中的任何行时,publicationsList_SelectionChanged 事件处理程序会告诉我选择的是网格控件而不是列表框控件。这不允许我使用列表框的 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

【问题讨论】:

  • 不要在 WPF 的过程代码中创建或操作 UI 元素。这就是 XAML 的用途。使用ItemsControl。顺便说一句,XAML 已经支持 DataBinding 到 XML。你正在重新发明轮子,你正在使它成为方形。删除所有代码并重新开始。
  • 发布您需要的屏幕截图以及您的Publications 类的完整代码,我可以告诉您在 WPF/XAML 中执行此操作的正确方法。
  • 您将此标记为 2 种相似但通常不兼容的技术。它是哪一个? WPF 还是电话?
  • @WiredPrairie 它的电话。
  • 我已经用出版物和出版物类更新了帖子,还链接了一个屏幕截图示例。

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


【解决方案1】:

当你说publicationsListSelectionChanged 事件处理程序告诉我选择的是网格控件而不是列表框控件时,我不明白你的意思,因为这完全没有意义。为了访问SelectionChanged 处理程序中的ListBox,您只需将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]);
    ...
}

更新>>>

我不明白为什么您的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]);
    ...
}

【讨论】:

  • 谢谢。但我在“Publication selectedItem = listBox.SelectedItem;”这一行得到一个错误无法将类型“对象”隐式转换为 FolktalesMoonlightMobile.Publication
  • 尝试将listBox.SelectedItem 对象转换为Publication...查看我的编辑。
  • 感谢您的更改。它现在不会在代码中出错,而是在运行时出错。此行的错误 Publication selectedItem = (Publication)listBox.SelectedItem;现在是“无法将'System.Windows.Controls.Grid'类型的对象转换为'FolktalesMoonlightMobile.Publication'。”感谢您迄今为止的帮助。
猜你喜欢
  • 2020-04-20
  • 1970-01-01
  • 2016-08-15
  • 2012-04-20
  • 2019-04-29
  • 1970-01-01
  • 2014-05-03
  • 2014-10-23
  • 1970-01-01
相关资源
最近更新 更多