0

I have a TabControl with a number of TabItems based on template. Each TabItem has a ListView inside, and I would like to get SelectedItems.

I use Binding for all of my ListViews, except for SelectedItems, where I use the SelectionChanged event :

private void ListView3_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   contexte.SelectedProfiles = listViewProfiles.SelectedItems.Cast<Profil>().ToList();
}

This works without problem, but when I use DataTemplate, the name of my listView is not recognized (I guess because I cannot give a name to a DataTemplate Listview, as there are several of them).

Here is my XAML for TabControl :

<TabControl x:Name="tabControl" ItemsSource="{Binding MyFullList}" SelectedIndex="{Binding SelectedGroupResult}">
    <TabControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Grid>
                    <Grid.Style>
                        <Style TargetType="Grid">
                            <Setter Property="Focusable" Value="false"/>
                        </Style>
                    </Grid.Style>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="auto"/>
                        <ColumnDefinition Width="auto"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="1" Text="{Binding Groupe}" Margin="3" />
                </Grid>

            </StackPanel>
        </DataTemplate>
    </TabControl.ItemTemplate>
    <TabControl.ContentTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto"/>
                    <RowDefinition Height="auto"/>
                    <RowDefinition Height="auto"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="auto"/>
                    <ColumnDefinition Width="auto"/>
                </Grid.ColumnDefinitions>
                <ListView x:Name="listViewProfiles" ItemsSource="{Binding ListPofils}" SelectedItem="{Binding SelectedProfile}" Grid.Row="2" Grid.ColumnSpan="2" Margin="3" Width="auto" SelectionMode="Extended" SelectionChanged="SelectionProfilChanged">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Header="{x:Static p:Resources.Name}" Width="auto" DisplayMemberBinding="{Binding Name}"/>
                        </GridView>
                    </ListView.View>
                </ListView>
            </Grid>
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

How can I get the Selected items from the ListView?

KJSR
  • 1,679
  • 6
  • 28
  • 51
Siegfried.V
  • 1,508
  • 1
  • 16
  • 34
  • @Babbillumpa it seems you're right, I will first check then validate duplicate, thanks – Siegfried.V Aug 08 '19 at 10:14
  • This would be quite an easy task using the MVVM pattern. You'd have a collection of viewmodels for each tab item, and every tab item view model would have a collection of viewmodels for each listview element. Then, with LINQ, you could simply do a double select on the items and get all of them. – Guido Cardinali Aug 08 '19 at 10:16
  • @Guido I use MVVM, as explained except for `SelectedItems`, I read on a post that `SelectedItems` is ReadOnly, so I cannot bind it directly to my ObservableCollection. Or do you mean this is possible? – Siegfried.V Aug 08 '19 at 10:19
  • 1
    Sorry, missed that part. AFAIK it's possible, we implemented it in our solution. Try checking this out: http://grokys.blogspot.com/2010/07/mvvm-and-multiple-selection-part-iii.html – Guido Cardinali Aug 08 '19 at 10:22
  • @GuidoC. thanks, had a look to it, but seems not so easy thing (but I save your link in favourites, will take a look obviously). For now the link from Babbillumpa made the job. – Siegfried.V Aug 08 '19 at 10:30
  • @Babbilumpa deleted my comment, cause there was a wrong information and couldn't edit it : the link you gave works perfectly, I believed that function `FindVisualChildren` returns all children, but I was wrong, and (as says the function's name), it returns only visible items... – Siegfried.V Aug 08 '19 at 10:51

0 Answers0