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