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