locked
How to bind dataform to viewmodel RRS feed

  • Question

  • Hi, I am using this:

     CurrentItem="{Binding Source={StaticResource Locator},Path=MyviewModel.MemberSearch,Mode=TwoWay, NotifyOnValidationError=True}"

    to bind to my viewmodel, MemberSearch is a property on the viewmodel which is a class. Everything works fine, now I need the viewmodel
    to implement IEditableObject, so now it's not picking up the IEditableObject because the binding of the currentitem is not to the viewmodel
    and rather is to that MemberSearch property. What is the correct way to bind to the viewmodel and specify which property to set the currentitem to?
    Thanks.

    Friday, January 6, 2012 7:11 PM

Answers

  • Hi,

     

    1. Import your ViewModel like this

    xmlns:vm="clr-namespace:SilverlightApplication4.ViewModel"

    2. Set the DataContext of the Grid

    <Grid x:Name="LayoutRoot">

       <Grid.DataContext>

          <vm:HomeViewModel />

       </Grid.DataContext>

    3. Bind the CurrentItem

    <dataForm:DataForm HorizontalAlignment="Left" Header="Account Search" Margin="20" Background="Transparent" Width="600"  

    Name="MemberSearchDataForm"  CommandButtonsVisibility="commit" 

    CurrentItem="{Binding Path=MemberSearch, Mode=TwoWay, NotifyOnValidationError=True}"             

    CommitButtonContent="Search" AutoGenerateFields="False"></dataForm:DataForm>

    Thanks,

    Bimal

    Saturday, January 7, 2012 3:38 PM
  • so If I did something like this (code below) How do i bind to the currentitem?

     

    Once you have set the LayoutRoot DataContext to point your viewmodel instance (through the Locator, since you seem to use one), then any binding is very straight, to take your own code sample, you can now write something like :

    <TextBox x:Name="Mytextbox" Text={Binding MemberSearch} />

    it is like you viewmodel name is implicit. All becomes very simple. You are certainly looking in your mind for something much more sophisticated while all become simple :-)

    (and do not import your viewmodel as one of our friend is saying, because your code is using a Locator and you must keep the Locator and not address the viewmodel directly).

    Saturday, January 7, 2012 6:03 PM

All replies

  • Hi,

         Bind an instance of your ViewModel to the DataContext of main control. For eg UserControl. Once it is done you will get all the properties in the ViewModel in that UserControl.

     

    Thanks,

    Bimal

    Friday, January 6, 2012 7:16 PM
  • Hi, thanks fro the reply, so If I did something like this (code below) How do i bind to the currentitem?

    <Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource Locator},Path=MyViewNodel,Mode=TwoWay}"  >
       <dataForm:DataForm HorizontalAlignment="Left" Header="Account Search"  
                               Margin="20" 
                               Background="Transparent"
                               Width="600" 
                               Name="MemberSearchDataForm" 
                               CommandButtonsVisibility="commit"
                               CurrentItem=?
                               Grid.ColumnSpan="4"  
                               CommitButtonContent="Search"  
                               AutoGenerateFields="False">
    Friday, January 6, 2012 7:23 PM
  • Hi,

     

    1. Import your ViewModel like this

    xmlns:vm="clr-namespace:SilverlightApplication4.ViewModel"

    2. Set the DataContext of the Grid

    <Grid x:Name="LayoutRoot">

       <Grid.DataContext>

          <vm:HomeViewModel />

       </Grid.DataContext>

    3. Bind the CurrentItem

    <dataForm:DataForm HorizontalAlignment="Left" Header="Account Search" Margin="20" Background="Transparent" Width="600"  

    Name="MemberSearchDataForm"  CommandButtonsVisibility="commit" 

    CurrentItem="{Binding Path=MemberSearch, Mode=TwoWay, NotifyOnValidationError=True}"             

    CommitButtonContent="Search" AutoGenerateFields="False"></dataForm:DataForm>

    Thanks,

    Bimal

    Saturday, January 7, 2012 3:38 PM
  • so If I did something like this (code below) How do i bind to the currentitem?

     

    Once you have set the LayoutRoot DataContext to point your viewmodel instance (through the Locator, since you seem to use one), then any binding is very straight, to take your own code sample, you can now write something like :

    <TextBox x:Name="Mytextbox" Text={Binding MemberSearch} />

    it is like you viewmodel name is implicit. All becomes very simple. You are certainly looking in your mind for something much more sophisticated while all become simple :-)

    (and do not import your viewmodel as one of our friend is saying, because your code is using a Locator and you must keep the Locator and not address the viewmodel directly).

    Saturday, January 7, 2012 6:03 PM