3

This is how the xaml looks like:

<ListView HorizontalContentAlignment="Stretch"
                      x:Name="listViewMessages"
                      Grid.Column="1"
                      ItemsSource="{x:Bind MessageViewModel.Messages}"
                      Height="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ActualHeight}"
                      ItemClick="listViewMessages_ItemClick">
                <ListView.HeaderTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                ...
                            </Grid.ColumnDefinitions>
                        </Grid>
                    </DataTemplate>
                </ListView.HeaderTemplate>

                <ListView.ItemTemplate>
                    <DataTemplate x:DataType="local:Message">
                        <Grid HorizontalAlignment="{x:Bind Path=MineToHorizontalAlignment()}" Background="{x:Bind Path=MineBackgroundColor()}" CornerRadius="8" Margin="0,6,0,2" Padding="6">
                            <Grid.ColumnDefinitions>
                                ...
                            </Grid.ColumnDefinitions>

                            ...
                            <Button Grid.Column="5" Click="Button_Click">D</Button>
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

So when I will click the button I want to get the list view item of the clicked button.

How can I achieve this?

Edit: I changed the xaml example to a more specific one.

Claudiu HBann
  • 56
  • 1
  • 9

1 Answers1

3

You need to use the VisualTreeHelper Class to traverse the visual tree. Here is a C++/WinRT utility to walk the parents recursively:

template <typename T>
T GetParent(DependencyObject obj)
{
    if (!obj)
        return nullptr;

    auto parent = Microsoft::UI::Xaml::Media::VisualTreeHelper::GetParent(obj);
    if (!parent)
        return nullptr;

    auto parentAs = parent.try_as<T>();
    if (parentAs)
        return parentAs;

    return GetParent<T>(parent);
}

And it's C# counterpart for what it's worth:

public static T GetParent<T>(DependencyObject obj) => (T)GetParent(obj, typeof(T));
public static object GetParent(DependencyObject obj, Type type)
{
    if (obj == null)
        return null;

    var parent = VisualTreeHelper.GetParent(obj);
    if (parent == null)
        return null;

    if (type.IsAssignableFrom(parent.GetType()))
        return parent;

    return GetParent(parent, type);
}

So you would call it like this:

void MainWindow::Button_Click(IInspectable const& sender, RoutedEventArgs const&)
{
    auto listView = GetParent<Controls::ListView>(sender.try_as<DependencyObject>());
}
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • It works fine for me with sample code. Make sure all packages are up to date and post a full simple reproducing sample. – Simon Mourier Jul 17 '22 at 11:06
  • can you please add the namespaces too and headers? Maybe that's my problem. – Claudiu HBann Jul 17 '22 at 12:43
  • Ok so the problem is with the listviewitems, if i have a simple listview with listviewitems in it works just fine, but if i have the example from my question doesnt anymore – Claudiu HBann Jul 17 '22 at 13:39
  • I recreated the full example if you want to take a look here: [MediaFire](https://www.mediafire.com/file/30709vnssf2x3cb/Test.7z/file) – Claudiu HBann Jul 17 '22 at 15:38
  • Your code's OnClickButtonDownload works fine. I get ListViewItem and/or ListView. Make sure you update all nuget packages. – Simon Mourier Jul 17 '22 at 20:53
  • all the packages are up to date, did you tried my example from the link i provided and worked properly? – Claudiu HBann Jul 18 '22 at 09:18
  • the package in the project in the .7z were not the latest (but it also worked) https://i.imgur.com/8gRPo5E.png – Simon Mourier Jul 18 '22 at 09:39
  • Ok so now I see the problem, I get the listviewitem but when I try to set the selected item of the listview to the listviewitem that I got doesnt work. 0 errors, exceptions or something. – Claudiu HBann Jul 18 '22 at 10:08