开发者

Connecting selected row event to mvvmlight command

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜