Reputation: 13
I know this type of question has been answered previously, but I just started with WPF
a week ago. I have the following scenario where i need to bind a Textblock
's Text
property to it's parent Grid
's Width
property entirely from XAML. So, if the Gird
's Width
is 50
, then Textblock
's Text
value should be 50
as well. Below is my XAML :
<Grid x:Name=MyGrid" Margin="112,11,0,0">
<Rectangle x:Name="ColorRect" Fill="Blue" Width="10" HorizontalAlignment="Left"/>
<TextBlock x:Name="ValueTextBlock" Text=""/>
</Grid>
Is it possible to bind the Text
property of the Textblock
to the Grid
's(myGrid) Width
property and also include a %
at the end of the text ? Say, if Width
= 50
,then TextBlock
's Text
would be 50%
??
Any help will be appreciated as i am totally new to this.
Upvotes: 1
Views: 894
Reputation: 5738
Actually, it is very very easy to bind a property to a parent's property. Here's a sample :
<TextBlock Text="{Binding (Grid.Width),
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}}"/>
Hope this helps :)
Upvotes: 2