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. Initial width of control with Binding on Width

Initial width of control with Binding on Width

Scheduled Pinned Locked Moved WCF and WF
wpfhelpwcfquestion
4 Posts 3 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.
  • G Offline
    G Offline
    GDavy
    wrote on last edited by
    #1

    I have a form in which I create some controls dynamically and bind the width of some text controls to the width of other controls at the creation time of the form. The problem is that when the Window appears initially these text controls do not have a correct width. From the moment I resize the controls of which the width is bound to the text boxes, the text boxes take the correct size. Since these controls are created dynamically I create the binding in code and not in xaml, like this

                Binding widthBound = new Binding();
                widthBound.Source = referenceControl;
                widthBound.Path = new PropertyPath("Width");
                widthBound.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
    

    And I set the binding on the textblock like this:

                txtBlock.SetBinding(TextBlock.WidthProperty, widthBound);
    

    The referenceControl takes an initial width depending on the content of the control. So I presume that this initial width is calculated during creation and the PropertyChanged is not triggered then. Is there a trick to get this Width of the textboxes correct anyway? Thanks for any help, Davy

    J G 2 Replies Last reply
    0
    • G GDavy

      I have a form in which I create some controls dynamically and bind the width of some text controls to the width of other controls at the creation time of the form. The problem is that when the Window appears initially these text controls do not have a correct width. From the moment I resize the controls of which the width is bound to the text boxes, the text boxes take the correct size. Since these controls are created dynamically I create the binding in code and not in xaml, like this

                  Binding widthBound = new Binding();
                  widthBound.Source = referenceControl;
                  widthBound.Path = new PropertyPath("Width");
                  widthBound.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
      

      And I set the binding on the textblock like this:

                  txtBlock.SetBinding(TextBlock.WidthProperty, widthBound);
      

      The referenceControl takes an initial width depending on the content of the control. So I presume that this initial width is calculated during creation and the PropertyChanged is not triggered then. Is there a trick to get this Width of the textboxes correct anyway? Thanks for any help, Davy

      J Offline
      J Offline
      Jammer 0
      wrote on last edited by
      #2

      I'd wrap all of these into a layout object (like a stackpanel) and let the stackpanel manage the sizes.

      Jammer My Blog | Article(s)

      1 Reply Last reply
      0
      • G GDavy

        I have a form in which I create some controls dynamically and bind the width of some text controls to the width of other controls at the creation time of the form. The problem is that when the Window appears initially these text controls do not have a correct width. From the moment I resize the controls of which the width is bound to the text boxes, the text boxes take the correct size. Since these controls are created dynamically I create the binding in code and not in xaml, like this

                    Binding widthBound = new Binding();
                    widthBound.Source = referenceControl;
                    widthBound.Path = new PropertyPath("Width");
                    widthBound.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        

        And I set the binding on the textblock like this:

                    txtBlock.SetBinding(TextBlock.WidthProperty, widthBound);
        

        The referenceControl takes an initial width depending on the content of the control. So I presume that this initial width is calculated during creation and the PropertyChanged is not triggered then. Is there a trick to get this Width of the textboxes correct anyway? Thanks for any help, Davy

        G Offline
        G Offline
        Gideon Engelberth
        wrote on last edited by
        #3

        You may be better off binding to the ActualWidth property of the referenceControl. As I understand, there are 4 different properties involving width: MinWidth, MaxWidth, Width, and ActualWidth. The first three are settable and can influence the last one, but only ActualWidth can be used to determine the current size of the control.

        G 1 Reply Last reply
        0
        • G Gideon Engelberth

          You may be better off binding to the ActualWidth property of the referenceControl. As I understand, there are 4 different properties involving width: MinWidth, MaxWidth, Width, and ActualWidth. The first three are settable and can influence the last one, but only ActualWidth can be used to determine the current size of the control.

          G Offline
          G Offline
          GDavy
          wrote on last edited by
          #4

          Thanks for the suggestion, this sets the initial width correctly. Regards, Davy

          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