George H.
George H.

Reputation: 89

UWP access controls in ListViewItem from colletion

I have a ListView with two DataTemplate for items and headers. Items from the ListView are binded to CollectionViewSource which looks like this:

<CollectionViewSource
            x:Name="groupedItemsViewSource3"
            Source="{Binding Groups2}"
            IsSourceGrouped="true"
            ItemsPath="Items"
            d:Source="{Binding Groups, Source={d:DesignData Source=/DataModel/SampleData.json, Type=data:SampleDataSource}}"/>

I can manage to get ListViewItem but I cant get control of its child controls. My ListView looks like this:

<ListView
                                            Margin="0,40,0,0"
                                            Width="580"
                                            HorizontalAlignment="Right"
                                            x:Name="itemGridView1"
                                            AutomationProperties.AutomationId="ItemGridView"
                                            AutomationProperties.Name="Grouped Items"
                                            ItemsSource="{Binding Source={StaticResource groupedItemsViewSource2}}"
                                            SelectionMode="None"
                                            IsSwipeEnabled="false"
                                            IsItemClickEnabled="True"
                                            ItemClick="ItemView_ItemClick" Background="White">
                                            <ListView.ItemTemplate>
                                                <DataTemplate>
                                                    <Grid HorizontalAlignment="Left" Background="LightGray" Width="2500" Height="25">
                                                        <Border HorizontalAlignment="Stretch" BorderThickness="0,0,0,1" BorderBrush="Black">
                                                            <StackPanel Orientation="Horizontal">
                                                                <StackPanel Orientation="Horizontal">
                                                                    <TextBlock Text="{Binding Time}" Margin="10,0,0,0" Width="50" Style="{StaticResource BodyTextBlockStyle}" TextWrapping="NoWrap" />
                                                                    <TextBlock Text="{Binding LiveTime}" Foreground="{Binding LiveTimeBGColor}" Margin="10,0,0,0" Width="40" Style="{StaticResource BodyTextBlockStyle}" TextWrapping="NoWrap" />
                                                                    <TextBlock Text="{Binding TeamOne}" Margin="0,0,10,0" HorizontalTextAlignment="Right" Width="150" Style="{StaticResource BodyTextBlockStyle}" TextWrapping="NoWrap" />
                                                                    <Border Background="DarkGray" Width="35" Margin="0,0,2,0" Padding="15,0,0,0">
                                                                    <TextBlock Text="{Binding ScoreTeamOne}"  Width="30" Foreground="White" Style="{StaticResource BodyTextBlockStyle}" TextWrapping="NoWrap" />
                                                                    </Border>
                                                                    <Border Background="DarkGray" Width="35" Padding="15,0,0,0" Margin="2,0,0,0">
                                                                    <TextBlock Text="{Binding ScoreTeamTwo}" Foreground="White" Width="30" Style="{StaticResource BodyTextBlockStyle}" TextWrapping="NoWrap" />
                                                                    </Border>
                                                                    <TextBlock Text="{Binding TeamTwo}" Margin="10,0,0,0" HorizontalAlignment="Left" Width="150" Style="{StaticResource BodyTextBlockStyle}" TextWrapping="NoWrap" /> 
                                                                </StackPanel>
                                                            </StackPanel>
                                                        </Border>
                                                    </Grid>
                                                </DataTemplate>
                                            </ListView.ItemTemplate>

                                            <ListView.GroupStyle>
                                                <GroupStyle>
                                                    <GroupStyle.HeaderTemplate>
                                                        <DataTemplate>
                                                            <Grid Margin="0,0,0,2" Width="2500" Background="{Binding HeaderLiveBGColor}">
                                                                <Button Foreground="{ThemeResource ApplicationHeaderForegroundThemeBrush}"
                                                                AutomationProperties.Name="Group Title"
                                                                Click="Header_Click"
                                                                Style="{StaticResource TextBlockButtonStyle}" Width="2500">
                                                                    <StackPanel Orientation="Horizontal" Width="2500">
                                                                        <TextBlock Text="{Binding LeagueTitle}" Margin="10,0,0,0" Width="441.9" Style="{StaticResource BodyTextBlockStyle}" TextWrapping="NoWrap" />
                                                                    </StackPanel>
                                                                </Button>
                                                            </Grid>
                                                        </DataTemplate>
                                                    </GroupStyle.HeaderTemplate>
                                                </GroupStyle>
                                            </ListView.GroupStyle>
                                            <ListView.ItemsPanel>
                                                <ItemsPanelTemplate>
                                                    <ItemsWrapGrid GroupPadding="0,0,20,0" Orientation="Horizontal"/>
                                                </ItemsPanelTemplate>
                                            </ListView.ItemsPanel>
                                        </ListView>

Any idea how can I check if the rights child control was clicked? What I want to eventually achieve is the handle a click based on the controls of the ListViewItem clicked.

Upvotes: 1

Views: 365

Answers (1)

Xie Steven
Xie Steven

Reputation: 8591

To get the clicked item from ListViewItem

If you add break points to debug your code, you could know there's a ClickedItem in ItemClickEventArgs class object. The ClickedItem should be that you want.

Another way is using TwoWay Binding on the SelectedItem.

Both the two ways are included in the following code sample:

<Page.Resources>
    <CollectionViewSource
        x:Name="groupedItemsViewSource3"
        Source="{Binding Groups2}"
        IsSourceGrouped="true"
        ItemsPath="Items" />
</Page.Resources>
<Grid>
    <ListView
                                        Margin="0,40,0,0"
                                        Width="580"
                                        HorizontalAlignment="Right"
                                        x:Name="itemGridView1"
                                        AutomationProperties.AutomationId="ItemGridView"
                                        AutomationProperties.Name="Grouped Items"
                                        ItemsSource="{Binding Source={StaticResource groupedItemsViewSource3}}"
                                        SelectedItem="{Binding SelectedSong,Mode=TwoWay}"
                                        SelectionMode="Single"
                                        IsSwipeEnabled="false"
                                        IsItemClickEnabled="True"

                                        ItemClick="ItemGridView_ItemClick" Background="White">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid HorizontalAlignment="Left" Background="LightGray" Width="2500" Height="25">
                    <Border HorizontalAlignment="Stretch" BorderThickness="0,0,0,1" BorderBrush="Black">
                        <StackPanel Orientation="Horizontal">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Title}" Margin="10,0,0,0" Width="50" Style="{StaticResource BodyTextBlockStyle}" TextWrapping="NoWrap" />
                            </StackPanel>
                        </StackPanel>
                    </Border>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>

        <ListView.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <Grid Margin="0,0,0,2" Width="2500" Background="{Binding HeaderLiveBGColor}">
                            <Button Foreground="{ThemeResource ApplicationHeaderForegroundThemeBrush}"
                                                            AutomationProperties.Name="Group Title"

                                                            Style="{StaticResource TextBlockButtonStyle}" Width="2500">
                                <StackPanel Orientation="Horizontal" Width="2500">
                                    <TextBlock Text="{Binding Key}" Margin="10,0,0,0" Width="441.9" Style="{StaticResource BodyTextBlockStyle}" TextWrapping="NoWrap" />
                                </StackPanel>
                            </Button>
                        </Grid>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </ListView.GroupStyle>
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <ItemsWrapGrid GroupPadding="0,0,20,0" Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
    </ListView>
</Grid>
public sealed partial class MainPage : Page
{
    public ObservableCollection<SongGroup> Groups2 { get; set; }

    private Song _SelectedSong;

    public Song SelectedSong
    {
        get { return _SelectedSong; }
        set
        {
            _SelectedSong = value;
        }
    }

    public MainPage()
    {
        this.InitializeComponent();
        Groups2 = GenerateData();
        this.DataContext = this;
    }

    private ObservableCollection<SongGroup> GenerateData()
    {
        ObservableCollection<SongGroup> songGroups = new ObservableCollection<SongGroup>();

        ObservableCollection<Song> songs = new ObservableCollection<Song>();
        songs.Add(new Song() { Title = "Song1" });
        songs.Add(new Song() { Title = "Song2" });

        songGroups.Add(new SongGroup() { Key = "A", Items = songs });

        ObservableCollection<Song> songs2 = new ObservableCollection<Song>();
        songs2.Add(new Song() { Title = "Song2_1" });
        songs2.Add(new Song() { Title = "Song2_2" });
        songGroups.Add(new SongGroup() { Key = "B", Items = songs2 });

        return songGroups;
    }

    private void ItemGridView_ItemClick(object sender, ItemClickEventArgs e)
    {
        var song = e.ClickedItem;
    }
}

public class Song
{
    public string Title { get; set; }
}

public class SongGroup
{
    public string Key { get; set; }

    public ObservableCollection<Song> Items { get; set; }
}

Upvotes: 1

Related Questions