0

I have a program developed in WPF using MVVM pattern. In this XAML code, I have a ListView with multiple columns, and one of them has a clickable button to dispel an action to the ViewModel. The problem is that in the ViewModel I need to know what was the row.

The ListView already has SelectedItem binding property, but as I am clicking the button and not the line itself, the line is not marked as selected.

Can anyone tell me another way to obtain the clicked row? Thanks!

Interaction namespace declaration:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

ListView code:

<ListView x:Name="AndroidDescriptionsList" HorizontalAlignment="Stretch"  Grid.Column="0" Grid.Row="6"
    Margin="0" VerticalAlignment="Top" ItemContainerStyle="{DynamicResource boList}"
    SelectedItem="{Binding SelectedAndroidCountryDescription}" Grid.ColumnSpan="7">
    <ListView.View>
        <GridView ColumnHeaderContainerStyle="{StaticResource boListHeader}">
            <GridViewColumn Header="" Width="50">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <Button x:Name="DeleteAndroidCountryDescriptionBtn" Style="{StaticResource foButton.Standard.IconsTransparent}" MinWidth="40"  >
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="Click">
                                    <i:InvokeCommandAction Command="{Binding DataContext.DeleteAndroidCountryDescriptionBtn, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type origin:ProductCreateView}}}"/>
                                </i:EventTrigger>
                            </i:Interaction.Triggers>

                            <TextBlock TextWrapping="Wrap" Text="{x:Static TechUI:AppConstants+ICONS.Error}" Foreground="#CCE74C3C" FontSize="20" Margin="0" Grid.Row="1"/>
                        </Button>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn DisplayMemberBinding="{Binding Country_Name}" Header="País" Width="150"/>
            <GridViewColumn DisplayMemberBinding="{Binding Country_Short_Name}" Header="Abreviatura" Width="100"/>
            <GridViewColumn DisplayMemberBinding="{Binding Description}" Header="Descrição" Width="500"/>
        </GridView>
    </ListView.View>
</ListView>  
dymanoid
  • 14,771
  • 4
  • 36
  • 64
Catarina Ferreira
  • 1,824
  • 5
  • 17
  • 26
  • 2
    Bind the CommandParameter to the current DataContext: `CommandParameter="{Binding}"` – Clemens Aug 08 '19 at 09:46
  • And why are you using InvokeCommandAction when the Button already has a Command and a CommandParameter property? – Clemens Aug 08 '19 at 09:49
  • It works perfectly! Thank you! It's not what I expected because I wanted that the Binding of SelectedItem worked as normal, but the current row is sent as argument, so it's enough. – Catarina Ferreira Aug 08 '19 at 09:52
  • This project is in MVVM and I use Caliburn. I could use that, and I could use cal:messageAttach too, but for some reason this actions crash in Windows XP, and this project need's to work on it. – Catarina Ferreira Aug 08 '19 at 09:56

0 Answers0