Results 1 to 2 of 2

Thread: [RESOLVED] Properly design a user control?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Resolved [RESOLVED] Properly design a user control?

    Hi!

    I have created a very basic user control. It has 3 textboxes andits purpose is to present a string that is split up into three different values. Say the string is xxx-AAAA-BBB. Then the user control will split this string and present it's values in different textboxes. There should be a binding also so if the user changes the text, it should be preserved and put back into the single string when user clicks save. We use MVVM in the application. Here are my thoughts:

    1) Create a user control with a viewmodel class that has 4 properties (part1, part2, part3 and WholeFormattedString).

    2) part 1-3 will be set in the constructor and are based from the WholeFormattedString. The property WholeFormattedString has a get and set property and the Get simply puts part1-3 together and return a string. The set splits the incoming string in 3 strings and set the private members that are bound to the textboxes. It is the parent that fetches the value from the db and should just pass it along to the user controls viewmodel.

    3) Does the usercontrol really need a viewmodel? it makes sence since there is logic involved in splitting the string and this UC should be used on many different forms.

    Is there a better way to solve this? The binding seems pretty basic, Im more concerned about how to assign the values from the parent view to the usercontrol view. And also I need a binding between the usercontrol WholeFormattedString and the parentview.WholeFormattedString since it is the parentview.WholeFormattedString that is used when the user presses save on the form.


    kind regards
    Henrik

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Properly design a user control?

    The UC definitely doesn't need four properties. It needs either one or three.

    One option would be to consider the splitting and recombining of the text as a presentation-only issue and do it in the UC itself. In that case the UC needs just the one property and that can be bound to the window's view model so no need for a dedicated view model for the UC.

    Another option would be for the UC to expose one property per TextBox and those three properties get bound to a dedicated view model. That view model has a fourth property that is exposed to the parent view model to deal with the combined text value.

    Which of those two options you choose really depends on whether you consider the display of data in three separate parts to be a presentation issue alone or not, something like formatting a date/time value.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width