0

I've a GridView page with different elements (ItemExplorer). Each element has two TextBlocks (Name & Description) and they're binding to a Collection.

I want that to when I click in a single element, a new page should be opened with the item details (ItemViewer), like a name or description...

I'm new in C#, so I appreciate any help or support! :)

This is my current code:

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();
            }
        }
    }
}

ItemViewer.xaml

<Grid Height="225">
    <TextBlock x:Name="Item-Name" Text=""/>
    <TextBlock x:Name="Item-Description" Text=""/>
</Grid>
4

1 回答 1

1

Set in GridView:

<GridView IsItemClickEnabled="True" ItemClick="ItemClicked" ItemsSource="{Binding ItemList}">

in 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);
    }
}

in ItemViewer.xaml.cs

 protected override void OnNavigatedTo(NavigationEventArgs e)
{
 ItemCollection myElement = e.Parameter as ItemCollection;
 ...
}

Instead of public ItemCollection MyItemCollection;
i'd just use public ObservableCollection<FileModel> Items;

I hope it will help you somehow. However I encourage you to look about MVVM pattern. It is really useful in developing WPF/WinRT applications. (check also MVVM Light framework which gives you for instance Messanger class - which have ability to "send objects/messages" between classes).

于 2013-02-20T15:10:55.717 回答