views:

513

answers:

4

Is it possible to edit data in a SilverLight Child window when using RIA Services and Silverlight 4? It sounds like a simple enough question, but I have not been able to get any combination of scenarios to work.

Simply put, I am viewing data in a grid that was populated through a DomainDataSource. Instead of editing the data on the same screen (this is the pattern that ALL of the Microsoft samples seem to use), I want to open a child window, edit the data and return. Surely this is a common design pattern.

If anyone knows of a sample out there that uses this pattern, a link would be much appreciated.

Thanks, Rick Arthur

A: 

Hi Rick, Iam currently trying to do the same as you, with RIA and SL4. Did you fin a good solution to this or code/samples?

My problem is to communicate from one viewmodell to the other which object in my collection that is the "currentItem" - so that the Edit page is filled with the object to edit...

Mcad001
A: 

That is simple as you can pass the parameter to the other page in query string by using the following method in an event handler (edit_ButtonClick) shown below: private void NavigateToEditEvent(string eanCode) { NavigationService.Navigate(new Uri("/EditProduct?EANCode=" + eanCode, UriKind.Relative)); }

    private void editButton_Click(object sender, RoutedEventArgs e)
    {
        Product product = productDataGrid.SelectedItem as Product;
        if (product != null)
        {
            NavigateToEditEvent(product.EANCode);
        }

}

In the edit page do the following:

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        string eanCode = NavigationContext.QueryString["EANCode"];
        productDomainDataSource.FilterDescriptors.Add( 
            new FilterDescriptor ( "EANCode", FilterOperator.IsEqualTo, eanCode ) );

    }

The problem is you don't want to select a row and then click a button. I have used "datagrid_IsEditing" event successfully. But it has some behavioural issue.

You need to have a new datacontext. set it to current record by using the value in the query string and then try to save it using submit changes.

Another option would be to use dataform and datafields.

Both options throw exceptions, even though they are working on a different datacontext.

I am retrying those options today after a while and I need it fixed. Any help would be appreciated.

bsaran
A: 

This is a Microsoft sample that uses a ChildWindow. It uses RIA services, but not MVVM.

It doesn't fix a problem I'm having where entities get attached to my context before I want them to be, but does what you're looking for other than that.

Here's the relevant code to save you downloading the zip:

private void addNewEmployee_Click(object sender, RoutedEventArgs e)
    {
        EmployeeRegistrationWindow addEmp = new EmployeeRegistrationWindow();
        addEmp.Closed += new EventHandler(addEmp_Closed);
        addEmp.Show();
    }

public partial class EmployeeRegistrationWindow : ChildWindow
    {
        public EmployeeRegistrationWindow()
        {
            InitializeComponent();
            NewEmployee = new Employee();
            addEmployeeDataForm.CurrentItem = NewEmployee;
            addEmployeeDataForm.BeginEdit();    
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            addEmployeeDataForm.CommitEdit();
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            NewEmployee = null;
            addEmployeeDataForm.CancelEdit();
            this.DialogResult = false;
        }

        public Employee NewEmployee { get; set; }
    }
Simon_Weaver