I have a grid whose width is "1*". So the actual width decided at runtime I think. Within that grid I have another grid whose width I want to set to the runtime width of parent grid. How Can I do that in xaml through binding.
Asked
Active
Viewed 1.2e+01k times
60

Andrea Antonangeli
- 1,242
- 1
- 21
- 32

choudhury smrutiranjan parida
- 2,588
- 5
- 28
- 39
-
2No need to do that. A grid automatically sizes its children, including child grids. And when you say you have a Grid whose width is `1*` you certainly mean a Grid Column, don't you? You may post your XAML to make clear what you're actually doing. – Clemens Sep 18 '13 at 07:33
7 Answers
133
This will actually help you I guess
Width="{Binding ActualWidth, ElementName=parentElementName}"
This binds the width to the parent element or the element name provided

Anobik
- 4,841
- 2
- 17
- 32
-
2
-
1Binding the width to the parent widh seems to remove the ability to be able to update the contents via `ItemsSource`. See [the strange behaviour in this question](http://stackoverflow.com/questions/33136535/data-disappears-from-listview-after-assigning-new-data-to-itemssource). – user2609980 Oct 16 '15 at 15:06
52
This is generic solution which may work everywhere. You wouldn't need to write parent element name. This will identify its parent and will take parent's width.
Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ActualWidth}"

Rudresh Bhatt
- 1,935
- 2
- 23
- 29
28
I think that easiest way to do same thing is:
HorizontalAlignment="Stretch"
That is not using binding as you have been asked, but it is easier.

RockLegend
- 497
- 4
- 9
-
Much easier for my needs: put these into a grid, control widths with column definitions, HorizontalAlignment = Stretch, Profit. Thanks. – Jason P Sallinger Mar 11 '16 at 15:15
-
1The drawback to this seems to be that a combination of column definitions like: `
`, , and ` ` will not respect the middle column's lack of an Auto property – Zachary Canann Nov 01 '17 at 02:33 -
6
Width="{Binding Width, RelativeSource={RelativeSource AncestorType={x:Type Parent}, Mode=FindAncestor}}"
if both controls DataContext is different.

Mahbubur Rahman
- 4,961
- 2
- 39
- 46

Gaurav Panwar
- 974
- 10
- 11
-
-
Yes, Parent is not supported in WPF project, it's for silverlight. For WPF you can use actual parent control like UserControl, DataGrid and Window etc instead of Parent. – Gaurav Panwar Jul 23 '18 at 13:52
4
If you are doing it in CodeBehind, this works for me. It has the added advantage that bindMe does not have to be a child of toMe:
public static void BindWidth(this FrameworkElement bindMe, FrameworkElement toMe)
{
Binding b = new Binding();
b.Mode = BindingMode.OneWay;
b.Source = toMe.ActualWidth;
bindMe.SetBinding(FrameworkElement.WidthProperty, b);
}
usage:
child.BindWidth(parent);

William Jockusch
- 26,513
- 49
- 182
- 323
0
HorizontalAlignment doesn't work for buttons in Xamarin, you can use HorizontalOptions instead
HorizontalOptions="Fill"

Grigory Zhadko
- 1,484
- 1
- 19
- 33

Ninad Kulkarni
- 61
- 1
- 11
-1
Use HorizontalContentAlignment="Stretch" on ListBox:
<ListBox HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<Image HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Source="TestImage.png"/>
<TextBlock HorizontalAlignment="Center" Text="Test Text"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
or if it is inside a Template use this:
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<Image Width="{TemplateBinding Width}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Source="TestImage.png"/>
<TextBlock HorizontalAlignment="Center" Text="Test Text"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

Cinorid
- 57
- 5