Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WCF and WF
  4. Swapping data binding at runtime [solved]

Swapping data binding at runtime [solved]

Scheduled Pinned Locked Moved WCF and WF
wpfwcfhelpquestiondiscussion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Phil J Pearson
    wrote on last edited by
    #1

    I have two data-bound text boxes. One is bound to a string and the other to a number. The 'default' binding is set in XAML. Under some circumstances I need to reverse the bindings at runtime (the string is usually a prefix but sometimes it's a suffix). I have tried the following code in my view model, called when the window is loaded:

    Binding stringBinding = BindingOperations.GetBinding(view.seqLeft, TextBox.TextProperty);
    Binding numberBinding = BindingOperations.GetBinding(view.seqRight, TextBox.TextProperty);
    view.seqLeft.SetBinding(TextBlock.TextProperty, numberBinding);
    view.seqRight.SetBinding(TextBlock.TextProperty, stringBinding);

    After that the code loads the properties to which the binding refers. [edit] The problem is that it doesn't work because GetBinding returns a reference to the existing binding. I would need to create a copy but that seems as if it would be loads of hassle. What have I missed? Is there a better way? The problem is just that SetBinding does nothing! If you ClearBinding and then GetBinding (as a check) you get the original binding back. If you make an entirely new binding and SetBinding (with or without ClearBinding first) you STILL have the original binding. There are no errors or exceptions - SetBinding is simply ignored! WHAT'S GOING ON?? :mad: :sigh: [/edit]

    Phil


    The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.

    modified on Thursday, March 25, 2010 1:04 PM

    K P 2 Replies Last reply
    0
    • P Phil J Pearson

      I have two data-bound text boxes. One is bound to a string and the other to a number. The 'default' binding is set in XAML. Under some circumstances I need to reverse the bindings at runtime (the string is usually a prefix but sometimes it's a suffix). I have tried the following code in my view model, called when the window is loaded:

      Binding stringBinding = BindingOperations.GetBinding(view.seqLeft, TextBox.TextProperty);
      Binding numberBinding = BindingOperations.GetBinding(view.seqRight, TextBox.TextProperty);
      view.seqLeft.SetBinding(TextBlock.TextProperty, numberBinding);
      view.seqRight.SetBinding(TextBlock.TextProperty, stringBinding);

      After that the code loads the properties to which the binding refers. [edit] The problem is that it doesn't work because GetBinding returns a reference to the existing binding. I would need to create a copy but that seems as if it would be loads of hassle. What have I missed? Is there a better way? The problem is just that SetBinding does nothing! If you ClearBinding and then GetBinding (as a check) you get the original binding back. If you make an entirely new binding and SetBinding (with or without ClearBinding first) you STILL have the original binding. There are no errors or exceptions - SetBinding is simply ignored! WHAT'S GOING ON?? :mad: :sigh: [/edit]

      Phil


      The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.

      modified on Thursday, March 25, 2010 1:04 PM

      K Offline
      K Offline
      koleraba
      wrote on last edited by
      #2

      Hi Have you tried to clear the binding first and then create new bindings in code? I think that should work. Uros

      P 1 Reply Last reply
      0
      • K koleraba

        Hi Have you tried to clear the binding first and then create new bindings in code? I think that should work. Uros

        P Offline
        P Offline
        Phil J Pearson
        wrote on last edited by
        #3

        See my edit to my original post.

        Phil


        The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.

        K 1 Reply Last reply
        0
        • P Phil J Pearson

          See my edit to my original post.

          Phil


          The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.

          K Offline
          K Offline
          koleraba
          wrote on last edited by
          #4

          I tried in simple example and it works fine. What are the variables view.seqLeft and view.seqRight? They should be the TextBox-es and not the properties in your view model to wich to TextBox-es are bind to

          P 1 Reply Last reply
          0
          • K koleraba

            I tried in simple example and it works fine. What are the variables view.seqLeft and view.seqRight? They should be the TextBox-es and not the properties in your view model to wich to TextBox-es are bind to

            P Offline
            P Offline
            Phil J Pearson
            wrote on last edited by
            #5

            They are TextBox elements.

            Phil


            The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.

            1 Reply Last reply
            0
            • P Phil J Pearson

              I have two data-bound text boxes. One is bound to a string and the other to a number. The 'default' binding is set in XAML. Under some circumstances I need to reverse the bindings at runtime (the string is usually a prefix but sometimes it's a suffix). I have tried the following code in my view model, called when the window is loaded:

              Binding stringBinding = BindingOperations.GetBinding(view.seqLeft, TextBox.TextProperty);
              Binding numberBinding = BindingOperations.GetBinding(view.seqRight, TextBox.TextProperty);
              view.seqLeft.SetBinding(TextBlock.TextProperty, numberBinding);
              view.seqRight.SetBinding(TextBlock.TextProperty, stringBinding);

              After that the code loads the properties to which the binding refers. [edit] The problem is that it doesn't work because GetBinding returns a reference to the existing binding. I would need to create a copy but that seems as if it would be loads of hassle. What have I missed? Is there a better way? The problem is just that SetBinding does nothing! If you ClearBinding and then GetBinding (as a check) you get the original binding back. If you make an entirely new binding and SetBinding (with or without ClearBinding first) you STILL have the original binding. There are no errors or exceptions - SetBinding is simply ignored! WHAT'S GOING ON?? :mad: :sigh: [/edit]

              Phil


              The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.

              modified on Thursday, March 25, 2010 1:04 PM

              P Offline
              P Offline
              Phil J Pearson
              wrote on last edited by
              #6

              The only thing wrong with my code was the TextBlock.TextProperty in the SetBinding calls! They should, of course, have been TextBox.TextProperty but I'd messed with it so long I wasn't seeing the wood for the trees.

              Phil


              The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups