Results 1 to 2 of 2

Thread: How to bind to usercontrol child controls from parent view?

  1. #1

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

    How to bind to usercontrol child controls from parent view?

    Hi!

    I spent some time googling but so far no luck finding a good article, which is strange since thi´s should be a really common topic.

    The situation is like this. In a current view+viewmodel there is a textbox in a grid column which is connected to a viewmodel property. Now they will rearrange things and this column now have to be two textboxes and a new property has appeared in the viewmodel. Currently they just do a simple binding in xaml to textbox.text={Binding Path=CurrentOrder.Order.ArticleNumber}

    What I want to do is to write a usercontrol that has two textboxes that I somehow can bind to the viewmodel properties just like above. But I can't figure out how to do this binding in the xaml

    The usercontrol will be used in many different views and the datacontext names may differ from place to place. So basically, what I really wanted is a way in xaml to write something like

    <uc:myUserControl TextBox1Text={Binding Path=CurrentOrder.Order.ArticleNumber} TextBox2Text={CurrentArticle.Article.Unit} DividorCharacter={Binding Path=Common.DividorChar}</>

    Is this the way to do it? The usercontrol doesn't have it's own viewmodel and should work like "plug and play", just connect textbox text properties to the properties in the viewmodel.

    Also the character to show between textbox1 and 2 should be configurable also, how can I add this in xaml? This seems like really basic stuff, but maybe I google the wrong stuff?

    /S

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: How to bind to usercontrol child controls from parent view?

    What I would do is to also create a ViewModel for that usercontrol, and in your parent view you just have to expose a ViewMode property for that usercontrol where it can bind into. Actually, just figured it out yesterday when trying to pass data between viewmodels. Will show you a short example in a while.

    EDIT:

    An implementation can be like this.
    Last edited by dee-u; May 16th, 2013 at 01:35 AM.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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