Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
I want to ask a question , maybe it is stupid , but I want to best solution , so here it is:

I have a mode-less dialogue in wpf, its owner is a window. when I click a button on the model-less dialogue I want to do something on a part of windows.

I tried a solution like that:
define a static methods of that owner window, and call that static method on every click of button on modelless dialog, but in this static methods I have no access to datagrid. (on every click I want to select different row in a datagrid which is on window) and I guess define a object of that window in click of dialog is not good idea.


how can I response the click in window ? how can I communicate between them and I should have access to controls in window?

could anyone tell me or direct me to solution?
Posted

In WPF you should have the data grid (including its selected index property) bound to a view model. Give the second form a reference to that view model instance and then it can simply set the property (which will cause the UI controls on the first form, and anything else that is watching it, to update).

For some types of change you might want to modify the underlying model, instead. But selecting a different row seems like a view model thing to me. And generally in WPF you should go through the view model, as all the update and binding mechanics are attached there and not normally to the model (though if one model is attached to by several VMs, it makes sense to have notify mechanics on it as well).
 
Share this answer
 
Comments
Alimjan Yasin 4-Aug-11 5:39am    
I think your solution is best way, but I don't understand much about view model though I used it as data source for datagrid. could you tell me how to set select a row or bring a rwo into scrollview through view model?

thanks!
BobJanova 4-Aug-11 13:16pm    
In WPF, you should use Binding=... in the XAML. You should be able to bind the SelectedIndex property of a data grid to an integer dependency property on the view model, so that setting that property from code will make the selected index change.
Alimjan Yasin 8-Aug-11 2:21am    
thanks for you kind help.
There is no a concept of modeless dialog in WPF, to the best of my knowledge. It must be the regular window, owned by a main window.

Your approach is not the best. The most robust approach is implementing appropriate interface in a windows class. In this way, another windows should get only the interface reference representing the other window, which improves the isolation of windows — only the interface required for collaboration is passed.

Also, there is a very convenient way to implement interface in the window class. Pay attention, a window class is declared as partial. You can add one more file representing the same class and add a separate partial declaration of it. The trick is: the inheritance list should be be repeated in all parts. You specify a base class in one part (it it already done in the auto-generated code), and one or more interfaces in the inheritance list in other parts.

In this way, you can create a separate file with partial class declaration containing only the interface to be implemented in the inheritance list and its implementation.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900