开发者

Connecting selected row event to mvvmlight command

开发者 https://www.devze.com 2023-03-11 15:38 出处:网络
I\'m writing WPF application, that\'s using MVVMLight. I have a DataGr开发者_高级运维id and I wanna connect event of selecting row to command. That\'s the easy part. The hard(for me of course ;]) part

I'm writing WPF application, that's using MVVMLight. I have a DataGr开发者_高级运维id and I wanna connect event of selecting row to command. That's the easy part. The hard(for me of course ;]) part is to get the entity that's connected with the selected row. How can I do that?


You have many ways of doing so.

The first one would be to pass the selected row as a command parameter. You can do this by XAML or code-behind.

<GridView x:Name="gv">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <i:InvokeCommandAction Command="{Binding SelectedRowCommand}"
                                   CommandParameter="{Binding Path=SelectedItem, ElementName=gv}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</GridView>

You can also create a selected item property in your view model and bind it to your control.

<GridView x:Name="gv" SelectedItem="{Binding SelectedRow, Mode=TwoWay}">
</GridView>
public class MyViewModel
{
    public RowType SelectedRow
    {
        get { return _selectedRow; }
        set
        {
            _selectedRow = value;
            // selection changed, do something here
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号