Andy Hough

Hi All,

I'm trying to get a sub control to resize dynamically in relation to its parent. The Xaml I'm using is

<Button Height="25" Width="50" Tag="TextBox" >
<TextBox Height="{Binding Path=Parent.Height, RelativeSource={RelativeSource Self}}"
Width="{Binding Path=Parent.Width, RelativeSource={RelativeSource Self}}" />
</Button>
 
This all works fine until I apply an Adorner to the Button and resize the button. The button happly resizes but the TextBox doesn't.
 
Can anyone tell me where I'm going wrong.
 
Thanks in advance.
 
Andy


Re: Windows Presentation Foundation (WPF) Resizing Sub controls within a control

lee d

Specifying AncestorType also in the Binding should solve it




Re: Windows Presentation Foundation (WPF) Resizing Sub controls within a control

Andy Hough

Hi Lee,

I tried changed to code to as follow:

<Button x:Key="TextBox" Height="25" Width="50" Tag="TextBox" >

<TextBox Height="{Binding Path=Height, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}" Width="{Binding Path=Width, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}" />

</Button>

and it still works in the same way, the outer control will resize and the inner control will stay the same size.

Could there be something else that I'm missing

Thanks again

Andy





Re: Windows Presentation Foundation (WPF) Resizing Sub controls within a control

Neil Mosafi

Try binding to the ActualHeight and ActualWidth properties instead. The ActualHeight and ActualWidth change when the layout engine resizes an element, the Height and Width will not




Re: Windows Presentation Foundation (WPF) Resizing Sub controls within a control

Andy Hough

Hi Neil,

I still have getting similar results if I use FindAncestor version I get the same results parent control resizes inner does not, when I use Parent.ActualHeight the inner control is not displayed (I think that this is because its getting 0 value).

I'm just wondering if I'm missing something, should do something equilevant to page.Bainding in ASP.Net after I have added the control to the canvas to re-establish the binding or does it happen by magic!

Thansk again for the help

Andy





Re: Windows Presentation Foundation (WPF) Resizing Sub controls within a control

Drew Marsh

May I ask why you don't just set Width and Height to Auto on the Text box and Horizontal/VerticalContentAlignment to Stretch on the Button You can also use Margin on the TextBox to add any additional space you would like to leave around it's edges.

Like so:

<Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="200" Height="100" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
<TextBox Text="Hello World " Margin="4,10,4,10"/>
</Button>

HTH,
Drew





Re: Windows Presentation Foundation (WPF) Resizing Sub controls within a control

Andy Hough

Thanks Drew thats spot on, works fine. I did know about the ContentAlignment properties.

Thanks Again

Andy