Answered WPF DataTemplating

  • Thursday, August 30, 2012 3:02 PM
     
     

    Hi guys,

    I'm fairly new to the whole WPF/MVVM concept and I've been trying to find examples into how to add, edit and delete items from a database which I managed to find (Finally!), using Data context (dbml).

    In his work, he used multiple windows for each process (for example, having a new window to add a client, a new window to view the clients using a data grid etc...), using the ShowDialog() method. eg;

    private void MenuItem_AddClient(object sender, RoutedEventArgs e)
     {

        AddClient addClient = new AddClient();
                addClass.ShowDialog();

    }

    But what I was wondering is, is it possible to use the data template method within the xaml and bind each of those windows into a main window (in the main window i just have a menu bar at the top)?

    Or do I have to do an alternative way, if so could some one give me some guidance?

    Like i said, I'm new to the world of WPF and I'm sure someone else has created a thread something similar and I'm just being silly and not searching for it in the correct way or used the right terminology. 

    Many thanks,

    Greg.

All Replies

  • Thursday, August 30, 2012 4:21 PM
     
     
    I'm a little unclear on what you are asking, but if I read you right you are trying to move popup windows into a main window via datatemplates.  You can do this by just having a contentcontrol in your main window and assign whichever viewmodel you want a datatemplate applied to, to the datacontext of the contentcontrol. 

    Michael Stacie

  • Friday, August 31, 2012 9:35 AM
     
     

    Hi gregory.bmclub,

    If you are to make a child windows you can do something like this.

    AddClient addClient = new AddClient();

    addClient.Owner = Window.GetWindow(this);

    /* use addClient.Owner = this; (if you are nt using any user controls)*/

    addClass.ShowDialog();

    Hope it helps.

    Regards

    Agrawal

    -------------------------------------------------------------------------

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".





  • Friday, August 31, 2012 1:17 PM
     
     

    Michael Stacie,

    Thanks for replying, much appreciated.  Unfortunately using the example given to me, it doesn't contain any view-models what so ever which i found quite odd myself personally, and just uses the code behind to have all the code.

    From what your suggesting is it still possible to so with the code behind?

    Cheers.

  • Friday, August 31, 2012 1:37 PM
     
     

    Agrawal,

    Thanks for the reply,

    From my understanding, what i am currently doing is a child window. I want a way of having a another view appear within a grid for example of the main window with the use of datatemplating... if that makes sense at all?

    Many thanks.

  • Friday, August 31, 2012 4:50 PM
     
     
    Can you plaese post the images of output you want?
  • Friday, August 31, 2012 9:28 PM
    Moderator
     
     Answered

    Hi greg, as the guys above say, if you show us some code examples and screen grabs we can better understand your problem.

     

    From what I understand, you want to move the contents of some windows into the main window, instead of popups. Just move all your code from the Window controls, to a UserControls (xaml to xaml, codebehind to codebehind). Windows and UserControls work the same. 

    Then you just add the UserControl to the Child property of a Border, or Children property of a grid, StackPanel, etc.

    An example of this is in the following project:

    http://code.msdn.microsoft.com/How-to-Build-Manage-and-fdd0074a (please rate it, if it helps :)

     

    Kind regards,
    Pete


    #PEJL

    • Marked As Answer by gregory.bmclub Monday, September 03, 2012 7:56 AM
    •  
  • Monday, September 03, 2012 8:49 AM
     
     

    Thanks Pete,

    Your a life saver. That's exactly what I wanted or would like to use for my application to help me navigate between different views,

    much appreciated thank-you.

    Gregz.