如何从GridView中的ItemCollection获取项目详细信息
本文关键字:获取 项目 详细信息 ItemCollection 中的 GridView | 更新日期: 2023-09-27 18:00:13
我有一个带有不同元素的GridView页面(ItemExplorer)。每个元素都有两个文本块(名称和描述),它们绑定到一个集合。
我希望当我单击单个元素时,应该打开一个包含项目详细信息(ItemViewer)的新页面,比如名称或描述。。。
我是C#的新手,所以我感谢任何帮助或支持!:)
这是我当前的代码:
ItemExplorer.xaml:
<GridView ItemsSource="{Binding ItemList}">
<GridView.ItemTemplate>
<DataTemplate>
<Border DoubleTapped="GoToItemViewer_DoubleTapped">
<StackPanel>
<TextBlock Name="ItemName" Text="{Binding ItemName}"/>
<TextBlock Name="ItemDescription" Text="{Binding ItemDescription}"/>
</StackPanel>
</Border>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
ItemExplorer.xaml.cs:
namespace Test001.Pages
{
public sealed partial class ItemExplorer : Page
{
public ItemCollection MyItemCollection;
public ItemExplorer()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
MyItemCollection = new ItemCollection();
this.DataContext = MyItemCollection;
}
// Go to ItemViewer
private void GoToItemViewer_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
if (this.Frame != null)
{
// SEND THE ITEM DETAILS AS PARAMTER TO ITEMVIEWER
this.Frame.Navigate(typeof(ItemViewer));
}
}
}
}
ItemCollection.cs
Collectionnamespace Test001.DataSource
{
public class CollectionFiles: BindableBase
{
private ItemCollection _ItemList = new ItemCollection();
public ItemCollection ItemList
{
get { return _ItemList; }
set { SetProperty(ref _ItemList, value); }
}
public CollectionFiles()
{
_ItemList.Add(new FileModel()
{
ItemName = "ItenName0",
ItemDescription = "Iten Description0",
});
_ItemList.Add(new FileModel()
{
ItemName = "ItenName1",
ItemDescription = "Iten Description1",
});
_ItemList.Add(new FileModel()
{
ItemName = "ItenName2",
ItemDescription = "Iten Description2",
});
_ItemList.Add(new FileModel()
{
ItemName = "ItenName3",
ItemDescription = "Iten Description3",
});
}
}
}
ItemViewer.xaml.cs
namespace mydox104.Pages
{
public sealed partial class DocumentViewer : mydox104.Common.LayoutAwarePage
{
public DocumentViewer()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// GET THE ITEM DETAILS
string file_name = e.Parameter as string;
if (!string.IsNullOrWhiteSpace(file_name))
{
pageTitle.Text = file_name;
}
else
{
pageTitle.Text = e.Parameter.ToString();
}
}
}
}
项目查看器.xaml
<Grid Height="225">
<TextBlock x:Name="Item-Name" Text=""/>
<TextBlock x:Name="Item-Description" Text=""/>
</Grid>
在GridView:中设置
<GridView IsItemClickEnabled="True" ItemClick="ItemClicked" ItemsSource="{Binding ItemList}">
在ItemExplorer.xaml.cs 中
private void ItemClicked(object sender, ItemClickEventArgs e)
{
var clickedItem = e.ClickedItem as ItemCollection;
if (clickedItem != null )
{
this.Frame.NavigateTo(typeof(ItemViewer), clickedItem);
}
}
在ItemViewer.xaml.cs 中
protected override void OnNavigatedTo(NavigationEventArgs e)
{
ItemCollection myElement = e.Parameter as ItemCollection;
...
}
而不是public ItemCollection MyItemCollection;
我就用public ObservableCollection<FileModel> Items
;
我希望它能对你有所帮助。然而,我鼓励您关注MVVM模式。它在开发WPF/WinRT应用程序时非常有用。(还可以查看MVVM Light框架,它为您提供了例如Messanger类,它能够在类之间"发送对象/消息")。