Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

WPF multiple bindings

Options
  • 20-12-2012 12:53am
    #1
    Registered Users Posts: 221 ✭✭


    Hi All,

    I'm kinda new to WPF and I've been working on a large project and getting on ok for the most part. In saying that I've recently hit a snag .

    The problem is I'm using MVVM pattern and I want to make an edit form. The edit Form will have a row of labels down the left with text bound to a data context. Alined with these labels are textboxes bound to a different data source.

    So for example

    we read a list of field names for one places (firstname, surname, postcode) and we read the details that they align with from another say a collection of type contacts ({"Joe", "Bloggs", '1000'})

    so we ened up with

    firstname - |Joe |
    surname - |Bloggs |
    postcode - |1000 |

    All of this needs to be dynamic and work regardless of if I change contact for another object type .

    Can anyone point me in the right direction of the best approach to achive this.

    Many thanks
    Tagged:


Comments

  • Registered Users Posts: 1,686 ✭✭✭RealistSpy


    Have you tried binding the textbox in xaml ItemsSource="{Binding Contact}"

    Loop //If needed
    Contact data = new Contact (Name, Surname, PC);
    TextBox1.Text = data.Name
    Etc

    I hope this helps a little.


  • Registered Users Posts: 11 SionnachRoe


    This can be done in a few ways though, without seeing the rest of the system, I can't say which is best for your case.

    At the core is the idea that the view binds only to a single viewmodel (datasource)

    If you have a fixed number of fields
    e.g.

    Label1 Value1
    Label2 Value2
    Label3 Value3


    then the viewmodel will have a fixed number of properties

    string Label1Text
    string Value1

    string Label2Text
    string Value2

    string Label3Text
    string Value3

    and each control binds statically to a single property on the viewmodel (note: there can be some messing around here if the values can be non-string types, but one problem at a time)

    There is no need for a second datasource in the XAML
    There are no loops


    A question is where/how these values are set. This is the variable part. We can set them in the viewmodel, passing in the two datasources (field names, and field values) or we can set them in the model (basically replicating the above values on the model and the viewmodel fields become simple passthroughs). Unfortunately, it depends on the rest of the system.

    One option:
    The Model contains the object to be bound.
    The ViewModel is created by passing in the object to be bound and we use some mechanism (say attributes or simply reflecting through the public properties) to set the values for the labels and bind the Value properties on the viewmodel to the properties of the object.



    If the number of fields varies then you will need to have a list of Label,Value pairs on the viewmodel and an items control in the Xaml which binds to the list with each item in the list binding to a Label,Value pair.


    Hope this is of help,
    Alan


    PS:

    Before putting in the work to try and implement this by hand I would have a search around for a WPF port of Silverlights DataForm control (WPF DataForm (CodePlex) or if you have a budget to buy a control (this will save a lot of development time) do a web search for WPF Dataform controls and have a look to see if any match your needs.


  • Registered Users Posts: 221 ✭✭Elfman


    Thanks for both of those replies ,
    especially SionnachRoe's, very helpful thanks a million.
    Going to take a look and get back to you.


    cheers
    Elfman


Advertisement