对于WinRT应用程序,我有一个GridView,其中项目被分组,以便每个组包含一个标题和他的组元素。
我想在应用栏中有一个按钮,将我的gridView中的所有项目传递到所选状态(带有紫色边框和复选框,就像我右键单击某个项目一样)。
我尝试将每个项目添加到GridView的SelectedItems列表中,但它没有做任何事情。
private void FavoriButton_Click_1(object sender, RoutedEventArgs e)
{
foreach (Categorie cat in coll)
itemGridView.SelectedItems.Add(cat);
}
有谁知道如何将网格视图的所有项目都放到selectedState(带紫色边框和复选框)?
这是代码
public sealed partial class HomePage : LayoutAwarePage
{
ObservableCollection<Categorie> coll = new ObservableCollection<Categorie>();
public HomePage()
{
this.InitializeComponent();
cvs1.Source = coll;
(itemGridView as ListViewBase).ItemsSource = this.cvs1.View.CollectionGroups;
}
async private void FillPage()
{
var categories = App.api.Categories_Get();
if (categories == null || categories.Count == 0)
return;
for (var i = 0; i < categories.Count; i++)
coll.Insert(i, categories[i]);
}
private void FavoriButton_Click_1(object sender, RoutedEventArgs e)
{
foreach (Categorie cat in coll)
{
itemGridView.SelectedItems.Add(cat);
}
}
et le XAML
<common:LayoutAwarePage
x:Class="NMA.Pages.HomePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:NMA"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:common="using:NMA.Common"
mc:Ignorable="d">
<common:LayoutAwarePage.Resources>
<CollectionViewSource x:Name="cvs1" ItemsPath="listArt" IsSourceGrouped="True" />
<DataTemplate x:Key="Standard250x250ItemTemplatePerso">
<Grid HorizontalAlignment="Left" Width="270" Height="210" VariableSizedWrapGrid.ColumnSpan="1" VariableSizedWrapGrid.RowSpan="1" local:Tilt.IsTiltEnabled="False" >
<Image Width="270" Height="210" Source="{Binding ImgArt}" CacheMode="BitmapCache" VerticalAlignment="Top"/>
</Grid>
</DataTemplate>
</common:LayoutAwarePage.Resources>
<Grid Background="Transparent" x:Name="MyGrid">
<Grid x:Name="NormalGrid">
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<local:VariableGridView
x:Name="itemGridView"
AutomationProperties.AutomationId="ItemGridView"
AutomationProperties.Name="Items"
Grid.RowSpan="2"
Padding="120,130,120,74"
ItemsSource="{Binding Source={StaticResource cvs1}}"
ItemTemplate="{StaticResource Standard250x250ItemTemplatePerso}"
IsSwipeEnabled="False"
IsItemClickEnabled="True"
Background="Transparent"
ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollMode="Disabled" SelectionMode="Multiple">
<local:VariableGridView.ItemsPanel >
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal" Background="Transparent" local:Tilt.IsTiltEnabled="False" Margin="0,0,100,0" />
</ItemsPanelTemplate>
</local:VariableGridView.ItemsPanel>
<local:VariableGridView.GroupStyle>
<GroupStyle >
<GroupStyle.HeaderTemplate>
<DataTemplate x:Name="MyDataTemplate">
<Button x:Name="HeaderButton" AutomationProperties.Name="MyHeaderButton" Click="HeaderButton_Click_1" Style="{StaticResource ButtonHeader_Style}" Content="{Binding NomCat}" FontSize="26" FontFamily="{StaticResource SegoeWPLight}" Margin="-24,0,0,20" Width="900" Background="Transparent">
</Button>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<VariableSizedWrapGrid ItemWidth="270" ItemHeight="210" Orientation="Vertical" Margin="0,0,-30,0" MaximumRowsOrColumns="4" Background="Transparent" Width="900">
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</local:VariableGridView.GroupStyle>
</local:VariableGridView>
</Grid>
</common:LayoutAwarePage>
非常感谢
答案 0 :(得分:3)
我实际上发现了它,我正在努力通过可视树来实现,而使用ItemContainerGenerator则很简单。
private void FavoriButton_Click_1(object sender, RoutedEventArgs e)
{
for( var i = 0 ; i<itemGridView.Items.Count ; i++)
{
(itemGridView.ItemContainerGenerator.ContainerFromIndex(i) as GridViewItem).IsSelected = true;
}
}
毕竟很容易。