开发者

WrapPanel width binding

How can I bind WrapPanel width to width of first column of the ListView. Both co开发者_如何学JAVAntrols are in user control.


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();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜