我正在编写 Windows Phone 8.1应用程序(WINRT)。我有一个Listview,显示分组的项目。其中的类别和子类别。用户可以选择任何子类别,并且必须保存到变量中。
XAML:
<SemanticZoom x:Name="CategorySemanticZoom"
IsZoomOutButtonEnabled="True"
CanChangeViews="True"
Grid.Row="1">
<SemanticZoom.ZoomedInView>
<ListView x:Name="Category_ListViewDetail" IsSwipeEnabled="True" IsTapEnabled="True"
IsItemClickEnabled="True"
IsZoomedInView="True"
ItemClick="Category_ListViewDetail_ItemClick">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock
Text="{Binding SubCategoryName}"
FontSize="26"
Margin="30,0,10,0"
FontWeight="ExtraLight"
Foreground="{StaticResource DefaultTheme_DarkBlueColor}"
SelectionHighlightColor="{StaticResource DefaultTheme_MediumBlueColor}" TextTrimming="CharacterEllipsis"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Border Background="{StaticResource DefaultTheme_DarkBlueColor}"
CornerRadius="6"
HorizontalAlignment="Left"
Margin="10,20,10,20"
Tapped="Border_Tapped"
VerticalAlignment="Center">
<TextBlock Text="{Binding CategoryName}"
FontSize="26"
Margin="10,4,10,4"
VerticalAlignment="Center"
FontWeight="ExtraLight"
Foreground="{StaticResource DefaultTheme_BluishWhite_LightColor}"
SelectionHighlightColor="{StaticResource DefaultTheme_LightTileBorder}" TextTrimming="CharacterEllipsis" />
</Border>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<VariableSizedWrapGrid Orientation="Vertical"
Margin="0 0 0 0"
ItemHeight="55"/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</ListView.GroupStyle>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
</SemanticZoom.ZoomedInView>
<SemanticZoom.ZoomedOutView>
<ListView x:Name="Category_ListViewSummary"
Background="LightGray"
IsZoomedInView="False"
Opacity=".85">
<ListView.ItemTemplate>
<DataTemplate>
<Border Background="{StaticResource DefaultTheme_DarkBlueColor}"
CornerRadius="6"
HorizontalAlignment="Left"
Margin="10,10,10,10" >
<TextBlock Text="{Binding Group.CategoryName}"
FontSize="26"
Margin="10,4,10,4"
VerticalAlignment="Center"
FontWeight="ExtraLight"
Foreground="{StaticResource DefaultTheme_BluishWhite_LightColor}"
SelectionHighlightColor="{StaticResource DefaultTheme_LightTileBorder}" TextTrimming="CharacterEllipsis" />
</Border>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
</SemanticZoom.ZoomedOutView>
</SemanticZoom>
CS:
private void GenerateCategoryListFromResultObject()
{
CollectionViewSourceObject = new CollectionViewSource();
CollectionViewSourceObject.IsSourceGrouped = true;
MasterCategory mcat = GetCategoriesResultObject.MasterCategories[0];
CollectionViewSourceObject.Source = mcat.Categories;
// CollectionViewSourceObject.ItemsPath = new PropertyPath("Contacts");
CollectionViewSourceObject.ItemsPath = new PropertyPath("SubCategories");
Category_ListViewDetail.ItemsSource = CollectionViewSourceObject.View;
Category_ListViewSummary.ItemsSource = CollectionViewSourceObject.View.CollectionGroups;
}
private void Border_Tapped(object sender, TappedRoutedEventArgs e)
{
CategorySemanticZoom.IsZoomedInViewActive = false;
}
string SelectedCat_SubCategoryName = "";
string SelectedCat_SubCategoryID = "";
private void Category_ListViewDetail_ItemClick(object sender, ItemClickEventArgs e)
{
SubCategory SubCategoryObject = (SubCategory)Category_ListViewDetail.SelectedValue;
SelectedCat_SubCategoryName = SubCategoryObject.SubCategoryName;
SelectedCat_SubCategoryID = SubCategoryObject.SubCategoryId.ToString();
if (SelectedCat_SubCategoryID == null || SelectedCat_SubCategoryID =="")
{
}
else
{
TapToSelectACategory_Underline_Run.Text = singletonInstance.TapToChangeCatString;
UserCategory_Selected.Text = SelectedCat_SubCategoryName;
}
Popup_Category.IsOpen = false;
}
数据类型/类别:
public class SubCategory
{
public int SubCategoryId { get; set; }
public int SubCategoryOrderId { get; set; }
public string SubCategoryName { get; set; }
}
public class Category
{
public int CategoryId { get; set; }
public int CategoryOrderId { get; set; }
public string CategoryName { get; set; }
public List<SubCategory> SubCategories { get; set; }
public Category()
{
this.SubCategories = new List<SubCategory>(); //instantiate
}
}
public class MasterCategory
{
public int MasterCategoryId { get; set; }
public int MasterCategoryOrderId { get; set; }
public string MasterCategoryName { get; set; }
public List<Category> Categories { get; set; }
public MasterCategory()
{
this.Categories = new List<Category>(); //instantiate
}
}
public class GetCategoriesResult
{
public bool status { get; set; }
public string ErrorMessage { get; set; }
public List<MasterCategory> MasterCategories { get; set; }
public GetCategoriesResult()
{
this.MasterCategories = new List<MasterCategory>(); //instantiate
}
}
问题在于,在选择任何子类别时,它总是在变量中显示第一个子类别:
SubCategory SubCategoryObject = (SubCategory)Category_ListViewDetail.SelectedValue;
SelectedCat_SubCategoryName = SubCategoryObject.SubCategoryName;
答案 0 :(得分:0)
private void Category_ListViewDetail_ItemClick(object sender, ItemClickEventArgs e)
{
//SubCategory SubCategoryObject = (SubCategory)Category_ListViewDetail.SelectedValue;
SubCategory SubCategoryObject = (SubCategory)e.ClickedItem;
}