1

In my project I used one button "Refresh" to refresh the DataGrid and registrtion from. This means when I insert a specific record that time it will insert into a database, and on the UI the Datagrid is not refreshing and the registration form is also maintaining filled information. Then if I click on the "refresh" button then the Datagrid and Registration form will also be refreshed. The problem is I am unable to write the refresh code in my Viewmodel. Therefore I wrote it in my Xaml.cs file. Here is my button click event:

Xaml.cs

 public void Button_Click(object sender, RoutedEventArgs e)
 {
     viewmodelObj = new AndazPatrakViewModel();
     viewmodelObj.BtnKharchKhVisibility = "Collapsed";
     viewmodelObj.BtnUtpannakhVisibility = "Visible";

     viewmodelObj.GetMaxId();
     this.DataContext = viewmodelObj;
 }

So in Viewmodel after Dbcontext.SaveChanges(); I want to call the Button_Click event shown above, but it asks for parameters for that function. How can i call this Button_Click event from my viewModel?

3
  • Put the code that is in the Button_Click in the ViewModel. And call it from the Button_click (or remove the button_click and assign a command to the button) This way you can call the method from with your viewmodel. Commented Mar 5, 2016 at 14:25
  • if i set Command for button .. then required output is not getting.. Commented Mar 6, 2016 at 11:11
  • Add properties for the 'parameters' to the viewmodel and bind the view to these properties. Commented Mar 6, 2016 at 14:56

1 Answer 1

1

It seems you already figured out how to get a reference to the code behind class.
So, since your Button_Click does not use its parameters, you could call it with null.

So, in your viewmodel:

codeBehindInstance.Button_Click(null, null);
Sign up to request clarification or add additional context in comments.

5 Comments

thank you @Domysee now it works... but my window is not refreshing.. can you tell me how can i do that..??
@RahulChaudhari you should have a look at INotifyPropertyChanged and implement it in your viewmodel. For collections, use ObservableCollection. If you did both, it will allow you to write the refresh code in your viewmodel. I answered a question just yesterday with a base implementation of INotifyPropertyChanged, here.
@RahulChaudhari why it doesnt work as it is now I'm not sure, I'd have to take a deeper look into your code.
Yes i used it.. I bind Itemsource one Observablecollection for my datagrid. and used INotifyPropertyChanged for same observablecollection
@RahulChaudhari if you like, you can ask a new question why the view doesnt update if you use INotifyPropertyChanged in your viewmodel. Its out of scope for this question, and the answer would probably be too long for a comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.