none
binding question

    Question

  • How to bind the replace the following binding syntax with code behind.

      <Grid DataContext="{Binding ElementName=ProductDDS, Path=Data}" HorizontalAlignment="Left" Margin="258,116,0,0" Name="grid1" VerticalAlignment="Top">

     

    I tried to amend this, but it did not work.

      <Grid DataContext="{Binding}" HorizontalAlignment="Left" Margin="258,116,0,0" Name="grid1" VerticalAlignment="Top">

     code behide.

    mycontext ctx = new mycontext();

    ctx.load(GetProductsQuery());

    grid1.datacontext=ctx.products

     

     Thanks in advance.

     

     

     

     

     

    Saturday, December 12, 2009 12:28 PM

Answers

  • Hi,

       You are using the DomainDataSource XAML Element, and so there is no direct replacement in Code-Behind, but what it does is the following:

       MyContext ctx = new MyContext();

       ctx.Load(ctx.GetProductQuery);

       Binding productBinding = new Binding();

       productBinding.Source = cts.Products;

       productBinding.Mode = BindingMode.TwoWay;

       grid1.SetBinding(Grid.DataContextProperty, productBinding);

       In XAML you only need to have the following:

        <Grid HorizontalAlignment="Left" Margin="258,116,0,0" Name="grid1" VerticalAlignment="Top">

    This should do the trick, since you don't need to do anything in the XAML or else your binding using the code-behing will be overriden.

    Hope this solves your problem.

    Saturday, December 12, 2009 1:10 PM
  • Hi,

       Sorry, the problem happens because you need to set the Path property of the Binding needs to be set, and in my sample I forgot that.

      So in order to do what you need you should do the following:

       MyContext ctx = new MyContext();

       ctx.Load(ctx.GetProductQuery);

       Binding productBinding = new Binding();

       productBinding.Source = ctx;

       productBinding.Path = new PropertyPath("Products");

       productBinding.Mode = BindingMode.TwoWay;

       grid1.SetBinding(Grid.DataContextProperty, productBinding);

    Now it works.

    Tuesday, December 15, 2009 7:03 AM

All replies

  • Hi,

       You are using the DomainDataSource XAML Element, and so there is no direct replacement in Code-Behind, but what it does is the following:

       MyContext ctx = new MyContext();

       ctx.Load(ctx.GetProductQuery);

       Binding productBinding = new Binding();

       productBinding.Source = cts.Products;

       productBinding.Mode = BindingMode.TwoWay;

       grid1.SetBinding(Grid.DataContextProperty, productBinding);

       In XAML you only need to have the following:

        <Grid HorizontalAlignment="Left" Margin="258,116,0,0" Name="grid1" VerticalAlignment="Top">

    This should do the trick, since you don't need to do anything in the XAML or else your binding using the code-behing will be overriden.

    Hope this solves your problem.

    Saturday, December 12, 2009 1:10 PM
  • Thanks for your information, but it generated the error message regarding the path propery if i set the binding mode in code-behide.

     

     

    Sunday, December 13, 2009 2:03 AM
  • Hi,

    What's the error message saying? You can set the path property when you initialize a binding object, like this:

    Binding productBinding = new Binding("Data");

    Tuesday, December 15, 2009 2:14 AM
  • Hi,

       Sorry, the problem happens because you need to set the Path property of the Binding needs to be set, and in my sample I forgot that.

      So in order to do what you need you should do the following:

       MyContext ctx = new MyContext();

       ctx.Load(ctx.GetProductQuery);

       Binding productBinding = new Binding();

       productBinding.Source = ctx;

       productBinding.Path = new PropertyPath("Products");

       productBinding.Mode = BindingMode.TwoWay;

       grid1.SetBinding(Grid.DataContextProperty, productBinding);

    Now it works.

    Tuesday, December 15, 2009 7:03 AM