简体   繁体   中英

Not getting value from StackPanel

I have been having a problem with getting the exact value from the stackpanel.

XAML

<Page
x:Class="MedicinesApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MedicinesApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
    <CollectionViewSource x:Name="MedicinesCollectionViewSource" IsSourceGrouped="True" />
</Page.Resources>
<GridView
    ItemsSource="{Binding Source={StaticResource MedicinesCollectionViewSource}}"
    x:Name="MedicinesGridView"
    Padding="30,20,40,0"
    SelectionMode="Single"
    IsSwipeEnabled="false"
    IsItemClickEnabled="True" SelectionChanged="MedicinesGridView_SelectionChanged"
    ItemClick="MedicinesGridView_ItemClick">
    <GridView.ItemTemplate>
        <DataTemplate x:Name="templateTrending">
            <Grid HorizontalAlignment="Left" Width="250" Height="250" x:Name="wow">
                <Border Background="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}">
                <Image Source="{Binding Path=DiseaseImageSource}" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}"/>
                </Border>
                <StackPanel VerticalAlignment="Bottom" Background="{ThemeResource ListViewItemOverlayBackgroundThemeBrush}">
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="Disease Name" Foreground="{ThemeResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextBlockStyle}" Height="30" Margin="15,0,15,0"/>
                        <TextBlock x:Name="DiseaseNameBro" Text="{Binding Path=DiseaseName, Mode=TwoWay}" Style="{StaticResource TitleTextBlockStyle}" Height="30" Margin="15,0,15,0"/>
                    </StackPanel>
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="Category of Disease" Foreground="{ThemeResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource CaptionTextBlockStyle}" TextWrapping="NoWrap" Margin="15,0,87,10"/>
                        <TextBlock Text="{Binding Path=CategoryOfDisease}" Foreground="{ThemeResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource CaptionTextBlockStyle}" TextWrapping="NoWrap" Margin="15,0,15,10"/>
                    </StackPanel>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </GridView.ItemTemplate>
    <GridView.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <TextBlock Text='{Binding Key}' Foreground="Gray" Margin="5" FontSize="30" FontFamily="Segoe UI Light" />
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
            <GroupStyle.Panel>
                <ItemsPanelTemplate>
                    <VariableSizedWrapGrid MaximumRowsOrColumns="2" Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </GroupStyle.Panel>
        </GroupStyle>
    </GridView.GroupStyle>
    <GridView.ItemsPanel>
        <ItemsPanelTemplate>
            <ItemsWrapGrid GroupPadding="0,0,70,0" />
        </ItemsPanelTemplate>
    </GridView.ItemsPanel>
</GridView>

C#

    using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Windows.UI.Xaml.Controls;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace MedicinesApp
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public string getMedicinesName;
        public ObservableCollection<Disease> diseaseList = new ObservableCollection<Disease>();
        public MainPage()
        {
            this.InitializeComponent();
            diseaseList.Add(new Disease { DiseaseName = "Malaria", DiseaseImageSource = "/Assets/133px-Malaria.jpg", CategoryOfDisease = "Blood" });
            diseaseList.Add(new Disease { DiseaseName = "Typhod", DiseaseImageSource = "../Assets/apple.jpg", CategoryOfDisease = "Thorat" });
            diseaseList.Add(new Disease { DiseaseName = "Cancer", DiseaseImageSource = "../Assets/orange.jpg", CategoryOfDisease = "Body" });
            diseaseList.Add(new Disease { DiseaseName = "Headache", DiseaseImageSource = "../Assets/bilberry.jpg", CategoryOfDisease = "Brain" });
            //diseaseList = new ObservableCollection<Disease>(diseaseList.OrderBy(c => c.DiseaseName));
            MedicinesCollectionViewSource.Source = GetGroupsByLetter();
        }
        internal List<GroupInfoList<object>> GetGroupsByLetter()
        {
            var groups = new List<GroupInfoList<object>>();
            var query = from item in diseaseList
                        orderby item.DiseaseName
                        group item by item.DiseaseName[0] into g
                        select new { GroupName = g.Key, Items = g };
            foreach (var g in query)
            {
                var info = new GroupInfoList<object>();
                info.Key = g.GroupName;
                foreach (var item in g.Items)
                {
                    info.Add(item);
                }

                groups.Add(info);
            }
            return groups;
        }
        public class GroupInfoList<T> : List<object>
        {
            public object Key { get; set; }

            public new IEnumerator<object> GetEnumerator()
            {
                return base.GetEnumerator();
            }
        }
        public class Disease
        {
            public string DiseaseName { get; set; }
            public string DiseaseImageSource { get; set; }
            public string CategoryOfDisease { get; set; }
        }
        private void MedicinesGridView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            getMedicinesName = ((Disease)MedicinesGridView.SelectedItem).DiseaseName;
        }
        private void MedicinesGridView_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.Frame.Navigate(typeof(malaria), getMedicinesName);
        }
    }

the problem is whenever I click on Malaria, 主页 it goes to the other page and shows cancer, which is the first vaule in the list. 我要在其中显示所选项目的疾病名称值的另一个页面 How can I get the value of the item I click and not of the first item. I Have attached screenshot for you'll to get a better knowledge of things.

I looked at your code and there is one thing that I don't understand. Why are you using 2 events to navigate. Only one would do the job.

    private void MedicinesGridView_ItemClick(object sender, ItemClickEventArgs e)
    {
        var selectedDisease = e.ClickedItem as Disease;
        this.Frame.Navigate(typeof(malaria), selectedDisease);
    }

You don't need the getMedicinesName either.

Don't forget to remove the SelectionChanged event both from you code behind and XAML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM