Search code examples
c#gridviewwin-universal-appwindows-10-mobilevisual-studio-2015

How to get selected controls from GridView with SelectionMode=Multiple


I have a GridView like this:

<GridView x:Name="list" ScrollViewer.VerticalScrollBarVisibility="Hidden" SelectionChanged="list_SelectionChanged" ItemsSource="{x:Bind Wallpapers}" SelectionMode="Multiple">
    <GridView.ItemTemplate>
        <DataTemplate x:DataType="data:ImageItem">
            <StackPanel>
                <Image FlyoutBase.AttachedFlyout="{StaticResource ImageMenuFlyout}" Width="150" Height="90" Source="{x:Bind img}" Tag="{x:Bind TagIndex}" Holding="Image_Holding" Tapped="Image_Tapped"/>
                <Image Source="used.png" Height="15" Margin="0,-15,0,0" HorizontalAlignment="Left" Width="44" Visibility="{x:Bind Used, Mode=OneWay}"/>
            </StackPanel>
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>

I need to get on the OnSelectionChanged method of the GrifView the visibility of the second Image control.

I need this because if the user select at least one item which have the second Image control visibility to "Visible", I have to enable a button.


Solution

  • Tried to recreate your example.

    I assume you have a class called ImageItem. I created one with only the Used property:

    public class ImageItem
    {
        public Visibility Used { get; set; }
    }
    

    My view is basically the same as yours, I used different ItemsSource name and only left in the Visibility binding of the second Image control:

    <GridView x:Name="list" ScrollViewer.VerticalScrollBarVisibility="Hidden" SelectionChanged="List_OnSelectionChanged" ItemsSource="{x:Bind Items}" SelectionMode="Multiple">
        <GridView.ItemTemplate>
            <DataTemplate x:DataType="local:ImageItem">
                <StackPanel>
                    <Image Width="150" Height="90" />
                    <Image Height="15" Margin="0,-15,0,0" HorizontalAlignment="Left" Width="44" Visibility="{x:Bind Used, Mode=OneWay}"  />
                </StackPanel>
            </DataTemplate>
        </GridView.ItemTemplate>
    </GridView>
    

    I bound it to the following collection:

    Items = new ObservableCollection<ImageItem>
    {
        new ImageItem() { Used = Visibility.Visible },
        new ImageItem() { Used = Visibility.Visible },
        new ImageItem() { Used = Visibility.Collapsed },
    };
    

    So only the third item's second Image is hidden.

    My selection changed handler is the following:

    private void List_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var gridView = sender as GridView;
    
        if (gridView != null)
        {
            var isAtLeastOneVisible =
                gridView.SelectedItems.OfType<ImageItem>().Any(i => i.Used == Visibility.Visible);
        }
    }
    

    So isAtLeastOneVisible will be true if you select at least the first and/or the second item in the list, and will be false if you only select the third item.