没有从StackPanel获得价值

时间:2016-06-18 06:39:03

标签: c# xaml windows-store-apps observablecollection listviewitem

我一直遇到从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);
        }
    }

问题是每当我点击疟疾, Mainpage 它转到另一页并显示癌症,这是列表中的第一个vaule。 Another Page where I want to display the value of the Disease Name of the selected item  如何获取我单击的项目的值而不是第一个项目的值。我已附上截图,以便更好地了解事物。

1 个答案:

答案 0 :(得分:0)

我看了你的代码,有一点我不明白。 为什么要使用2个事件进行导航。只有一个人会做这个工作。

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

您也不需要getMedicinesName。

不要忘记从你的代码和XAML中删除SelectionChanged事件。