views:

118

answers:

3

Hi,

How it is possible? The idea is to enter info into a form inside a pop-up/child window, and then, as you save/close it, it automatically updates the "Parent" window, from which the pop-up was originally open.

Kindly Suggest,

Thanks

A: 

Hope you would be having a ViewModel for the child window. It would be having all the data altered in the ChildWindow. Hence when you close the window, save the corresponding models to the DB and load your MainWindowView model collections again from the DB.

Veer
@Veer.Thanks for the reply. How to load MainWindowView model collection again from the database as the parent window is already running?Kindly Suggest?
Tarun
@Tarun: You can send a message to that ViewModel that the db has been changed using the Messenger helper class. Check this sample program http://blog.galasoft.ch/archive/2009/10/18/clean-shutdown-in-silverlight-and-wpf-applications.aspx
Veer
@Veer. In my parent Mainwindow.xaml,I have a datatemplate <DataTemplate x:Key="UserActivity"><TextBlock Text="{Binding UserActivityName}"/></StackPanel></DataTemplate>This is am calling in Listbox as ItemTemplate:<ListBox ItemTemplate="{StaticResource UserActivity}" Name="listBox1"/>in its code behind,i am setting listbox like this:Data_Access.DaoDailyActivities ActivityList = new Data_Access.DaoDailyActivities();listBox1.ItemsSource = ActivityList.GetUserActivity();How to do it xaml.means how i can use itemsource in xaml of listbox?
Tarun
`ObservableCollection<UserActivity> myOC = new ObservableCollection<UserActivity>(ActivityList.GetUserActivity());`In xaml: `<ListBox ItemsSource = "{Binding myOC}" />`
Veer
@Veer. I have tried the above but no values are coming in listbox. I have checked the above colection in debug mode,its displaying the values and all but its not binding in the xaml's ItemsSource of ListBox. It is working with in code behind file.Kindly Suggest?
Tarun
@Tarun: Have you set the DataContext of the View to the corresponding ViewModel. What does you ListView's ItemTemplate contain? If they are bound to ActivityViewModel then your OC must be collection of it rather. `ObservableCollection<UserActivity> myOC = new ObservableCollection<UserActivity>(); foreach(Activity a in ActivityList.GetUserActivity()) { myOC.Add(new ActivityViewModel(Activity, true)); }` Don't know if that is the signature of your ViewModel's constructor. Change it accordingly.
Veer
@veer. ok Will try n let u know
Tarun
@Veer. I have set the datacontext of the view. Listbox template only contains the textblock with binding with UserActivityName to show the activities in listbox.
Tarun
@Tarun: I want to know to which you are binding with. Did you use this code http://stackoverflow.com/questions/3164339/creating-a-dynamic-checkboxes-against-the-list-of-items-using-mvvm-light-wpf/3165615#3165615? Is you bound list an `ObservableCollection<ActivityViewModel>`? Share some code.
Veer
@Veer. Refreshing of parent window contents from child window is working now. By clicking Done button on child window,it gets closed and parents window content gets changed. But again after clicking Add New button on parent window, child window shows the previous state contents not the updated ones as in the parent window.
Tarun
A: 

In my app, I have a Singleton object with holds this event:

Public Event DataChanged(ByVal EntityChanged As String, 
                               ByVal IDChanged As Integer)

When a record is saved or deleted, the event is raised (in this case, by the pop-up window)

So all you have to do in the parent window (or in any other window), is subscribe to the event, and refresh the data when event is raised. You can check the EntityChanged param (Product, Order, Customer, etc.) and the ID to see if you must refresh or not.

Eduardo Molteni
How to use it exactly?Kindly Suggest?
Tarun
A: 

This is a perfect reason for using the Messenger class in MVVM Light. Create a NotificationMessage to send an update command to the main view model.

    public MainWindowViewModel()
    {
        if ((IsInDesignMode))
        {
        }
        // Code runs in Blend --> create design time data.
        else
        {
            // Code runs "for real"
            Messenger.Default.Register<NotificationMessage<string>>(this, new System.Action<NotificationMessage<string>>(NotificationMessageHandler));

        }
    }

    private void NotificationMessageHandler(NotificationMessage<string> sender)
    {
        if ((sender.Notification == "refreshData"))
        {
            // Do something here
        }
    }
Rick Ratayczak