60

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.

Andrea Antonangeli
  • 1,242
  • 1
  • 21
  • 32
  • 2
    No 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 Answers7

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
    What is ActualWidth property which isn't suggested by intelIsense? – Alex Jul 20 '15 at 19:36
  • 1
    Binding 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
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
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