Adam McMahon
Adam McMahon

Reputation: 800

How to get the Parent Item of the SelectedItem on ListView.SelectionChanged

Does anybody know how I can retrieve the selected Ellipse "bgColor" from the following code please? I can retrieve the Color, but I need to get the Ellipse which holds it.

XAML

            <ListView Name="BgColorList" Height="80" Width="850"
            ItemsSource="{Binding ColorList}"
            ItemContainerStyle="{StaticResource ListViewTransparent}"
            ScrollViewer.HorizontalScrollMode="Enabled"
            ScrollViewer.HorizontalScrollBarVisibility="Hidden"
            ScrollViewer.IsHorizontalRailEnabled="True" 
            IsItemClickEnabled="True"
            VerticalAlignment="Bottom"
            SelectionChanged="BgColorList_SelectionChanged"
            Margin="0,0,0,35">
                    <ListView.DataContext>
                        <local2:NamedColors />
                    </ListView.DataContext>
                    <ListView.ItemsPanel>
                        <ItemsPanelTemplate>
                            <ItemsWrapGrid />
                        </ItemsPanelTemplate>
                    </ListView.ItemsPanel>
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <Grid VerticalAlignment="Center" Margin="0,0,0,0" Height="65" Width="65">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <Ellipse Name="bgColor" Grid.Column="0" Height="50" Width="50" Margin="2" VerticalAlignment="Center" Stroke="Transparent" StrokeThickness="1">
                                    <Ellipse.Fill>
                                        <SolidColorBrush Color="{Binding }" />
                                    </Ellipse.Fill>
                                </Ellipse>
                            </Grid>
                        </DataTemplate>
                    </ListView.ItemTemplate>  
                </ListView>

C# - I don't seem to be able to find a way to retrieve the Ellipse here.

    private void BgColorList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ListView view = (ListView)sender;
        var selected = view.SelectedItem;
        Color selectedColor = (Color)view.SelectedItem;
    }

Upvotes: 0

Views: 711

Answers (2)

Nico Zhu
Nico Zhu

Reputation: 32775

How to get the Parent Item of the SelectedItem on ListView.SelectionChanged

For ListView, you could use ContainerFromItem method to get ListViewItem for SelectedItem, then use VisualTreeHelper to find the child element, but VisualTreeHelper will consume system performance. And for you scenario, the better way is listen Ellipse Tapped event as @Noorul said, when the item is clicked the Tapped even will invoke.

private void BgColor_Tapped(object sender, TappedRoutedEventArgs e)
{
    var ellipse = sender as Ellipse;
}

Upvotes: 0

mm8
mm8

Reputation: 169220

The ListView has a ContainerFromItem methid which should give you a reference to the ListViewItem container. You could then get a reference to the Ellipse using the VisualTreeHelper class:

private void BgColorList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ListView view = (ListView)sender;
    var selected = view.SelectedItem;
    var container = view.ContainerFromItem(selected);
    if (container != null)
    {
        Ellipse ellipse = FindVisualChild<Ellipse>(container);
        if (ellipse != null)
        {
            //...
        }
    }
}

private static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
{
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(obj, i);
        if (child != null && child is T)
            return (T)child;
        else
        {
            T childOfChild = FindVisualChild<T>(child);
            if (childOfChild != null)
                return childOfChild;
        }
    }
    return null;
}

Upvotes: 1

Related Questions