Binding a WrapPanel's ItemWidth to a Grid's Column width ...

I'm trying to use a WrapPanel as the ItemsHost for a ListBox. This works fine, but I'd like to bind the width of the WrapPanel's items (ItemWidth) to the width of a column in a Grid that contains the WrapPanel. I'm not sure if this is possible, or if I've gotten the data binding syntax incorrect. It's not working for me. Can anyone help Here's the XAML:

<ControlTemplate x:Key="ListBoxControlTemplate" TargetType="{x:Type ListBox}">

<Border CornerRadius="2" BorderBrush="Gray" BorderThickness="1">

<Grid>

<Grid.ColumnDefinitions>

<ColumnDefinition Width="*" />

<ColumnDefinition Width="*" />

</Grid.ColumnDefinitions>

<WrapPanel Grid.ColumnSpan="2" IsItemsHost="True" Orientation="Vertical" ItemHeight="45" ItemWidth="{Binding RelativeSource={RelativeSource AncestorType={x:Type Grid}}, Path=ColumnDefinitions[0].Width}" HorizontalAlignment="Left" VerticalAlignment="Top" />

</Grid>

</Border>

</ControlTemplate>



Answer this question

Binding a WrapPanel's ItemWidth to a Grid's Column width ...

  • Owen Evans

    One other thing, however - this works to initially set the itemwidth correctly according to the grid's column width. But it does not change the itemwidth when the grid is resized.

    But much better than before, and I can probably work with this result.

    Thanks again.


  • *Sarath*

    That was it - thanks so much!

    Sorry for the late response; I've been away for a few days.


  • chaosEngine

    replace

    ItemWidth="{Binding RelativeSource={RelativeSource AncestorType={x:Type Grid}}, Path=ColumnDefinitions[0].Width}"

    with

    ItemWidth="{Binding RelativeSource={RelativeSource AncestorType={x:Type Grid}}, Path=ColumnDefinitions[0].ActualWidth}"



  • gamer3100

    Did you try to set the HorizontalAlignment property or the WrapPanel to "Stretch" I think that's equal to width="100%" in html.

  • sursh

    ... doesn't appear to have any effect. Thanks for the suggestion.
  • Chris Darrigo

    Does anyone know if what I'm trying to do can even be accomplished Or do I just need to hard-code the widths and be done with it
  • Binding a WrapPanel's ItemWidth to a Grid's Column width ...