rysiard
rysiard

Reputation: 77

WrapPanel width binding

How can I bind WrapPanel width to width of first column of the ListView. Both controls are in user control.

Upvotes: 1

Views: 1003

Answers (1)

Fredrik Hedblad
Fredrik Hedblad

Reputation: 84684

For a GridViewColumn you have the ActualWidth property which isn't a Dependency Property so if you bind directly to it you won't get any updates when the property changes. Then you have the Width property which is a Dependency Property and will give you the correct value for most cases but not always e.g when autosizing. Combining these two should give you the desired result though. That is, listen to changes in Width property and return ActualWidth with a MultiBinding

Xaml

<WrapPanel ...>
    <WrapPanel.Width>
        <MultiBinding Converter="{StaticResource GridViewColumnWidthMultiConverter}">
            <Binding ElementName="listView" Path="View.Columns[0].Width"/>
            <Binding ElementName="listView" Path="View.Columns[0].ActualWidth"/>
        </MultiBinding>
    </WrapPanel.Width>
</WrapPanel>
<ListView Name="listView" ...>
    <ListView.View>
        <GridView>
            <GridView.Columns>
                <GridViewColumn .../>
                <GridViewColumn .../>
                <GridViewColumn .../>
                <!--...-->
            </GridView.Columns>
        </GridView>
    </ListView.View>
</ListView>

GridViewColumnWidthMultiConverter

public class GridViewColumnWidthMultiConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double width = (double)values[0];
        double actualWidth = (double)values[1];
        return actualWidth;
    }
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Upvotes: 1

Related Questions