Search code examples
c#wpfmvvmwpfdatagrid

Get selected item in a list from view model


Good morning!

I'm programming a managment aplication that shows a task list using WPF and MVVM patern. Users can edit this list too but I want to use a delete button to delete selected row. The problem is that I don't know how to access to the selected row from ModelView and I don't want to use code-behind. I guess the best option is using some binding but I'm unsure about how to do it.

This is the code ( I'm using DevXpress controls but is almost the same )

<dxg:GridControl  ItemsSource="{Binding Path=TaskView}" Loaded="GridControl_Loaded">
       <dxg:GridControl.Columns>
            <dxg:GridColumn Visible="False" DisplayMemberBinding="{Binding Path=ID, Mode=OneWay}" />
            <dxg:GridColumn Header="Project" DisplayMemberBinding="{Binding Path=ProjectCode, Mode=TwoWay}" AllowEditing="True" ContentChanged="OnHoursChanged"/>
            <dxg:GridColumn Header="Project Description" DisplayMemberBinding="{Binding Path=ProjectName, Mode=TwoWay}" AllowEditing="False"/>
            <dxg:GridColumn Header="Description" DisplayMemberBinding="{Binding Path=ProjectDescription, Mode=TwoWay}" AllowEditing="True"/>
            <dxg:GridColumn Header="Comment" DisplayMemberBinding="{Binding Path=Comment, Mode=TwoWay}" AllowEditing="True"/>
            <dxg:GridColumn Header="{Binding ElementName=mon,Path=Text}" DisplayMemberBinding="{Binding Path=HoursMonday, Mode=TwoWay}" AllowEditing="True" FieldName="M" />
            <dxg:GridColumn Header="{Binding ElementName=tue,Path=Text}" DisplayMemberBinding="{Binding Path=HoursTuesday, Mode=TwoWay}" AllowEditing="True" FieldName="T" />
            <dxg:GridColumn Header="{Binding ElementName=wed,Path=Text}" DisplayMemberBinding="{Binding Path=HoursWednesday, Mode=TwoWay}" AllowEditing="True" FieldName="W" />
            <dxg:GridColumn Header="{Binding ElementName=thu,Path=Text}" DisplayMemberBinding="{Binding Path=HoursThursday, Mode=TwoWay}" AllowEditing="True" FieldName="Th" />
            <dxg:GridColumn Header="{Binding ElementName=fri,Path=Text}" DisplayMemberBinding="{Binding Path=HoursFriday, Mode=TwoWay}" AllowEditing="True" FieldName="F" />
            <dxg:GridColumn Header="{Binding ElementName=sat,Path=Text}" DisplayMemberBinding="{Binding Path=HoursSaturday, Mode=TwoWay}" AllowEditing="True" FieldName="Sat" />
            <dxg:GridColumn Header="{Binding ElementName=sun,Path=Text}" DisplayMemberBinding="{Binding Path=HoursSunday, Mode=TwoWay}" AllowEditing="True" FieldName="Sun" />
            <dxg:GridColumn Header="Total" DisplayMemberBinding="{Binding Path=Total, Mode=OneWay}" AllowEditing="False" FieldName="Tot" />
        </dxg:GridControl.Columns>
    </dxg:GridControl>

Do you have any ideas?

Thank you very much!


Solution

  • Your VM should have a Project SelectedProject { get; set; } property.

    The bin the SelectedRow (or something) from the Grid to it:

    ItemsSource="{Binding Path=TaskView}" SelectedItem="{Binding Path=SelectedProject}"