开发者

List<> Binding and button click using mvvm light

开发者 https://www.devze.com 2023-01-05 03:34 出处:网络
I am trying to use MVVM light to achieve something like this. I have the following scenario: In my Model--I have set the properties like ActivityName,Image and there is a class constructor whose is a

I am trying to use MVVM light to achieve something like this. I have the following scenario:

In my Model--I have set the properties like ActivityName,Image and there is a class constructor whose is accepting 2 parameters like name and image.

Im my DataAccess--I have set the database connection and implement the required method who will fetch the data from DB and I am storing in the List and returning the list to ViewModel.

In my ViewModel--I have created the list property who will return the list by开发者_运维技巧 calling the GetActivities() method that I have defined in the DataAccess.

Now my problem is I am not getting how to bind it in the View so that by clicking on a button it will display the list of activities with image. By clicking on some button a new window should open with the desired result. How to bind the above list and implement the button functionality using MVVM light.

Kindly help?

Thanks


  • First of all, use an ObservableCollection instead of a List as it would notify the view when property or collection changes.
  • Then set the DataContext of your view to the viewmodel. If you use MVVMLight View Class, then the DataContext would be automatically set. You've to just give the ViewModel Name there.
  • Then set the ItemsSource of the DataGrid like this <dg:DataGrid ItemsSource="{Binding YourListInViewModel}"/>
  • For handling click event you can use the Event-To-Command behaviour and write your logics in the Button's corresponding Command handler.


Bind to DataContext of the control

0

精彩评论

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