1

I have a ListView control that has 3 columns: Name, Surname and address. I want all 3 columns to have the same width. The only way I came up with was to bind each column's width to the ActualWidth of the ancestor and use a convertor to divide this width by 3 as follows:

<GroupBox>
    <ListView Name="People" ItemsSource="{Binding peopleList}">
        <ListView.View>
                <GridView >
                        <GridViewColumn Header="name" DisplayMemberBinding="{Binding Name}">
                                <GridViewColumn.Width>
                                        <Binding Path="ActualWidth" Source="{RelativeSource Mode=FindAncestor, AncestorType={x:Type GroupBox}}" Converter ="{StaticResource ListViewConverter}"/>
                                    </GridViewColumn.Width>
            <GridViewColumn Header="surname" ...>
                ...
            </GridViewColumn>
            <GridViewColumn Header="address" ...>
                ...
            </GridViewColumn>
                            </GridViewColumn>
                    </GridView>
            </ListView.View>
   </ListView>
</GroupBox>

The convertion class is:

class ListViewConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        double state = (double)value;
        return state / 3;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}

But this doesn't work. I think that maybe the GroupBox is not an ancestor of GridViewColumn.Width. Maybe I got it all wrong and there is a much simpler way to do this?

George
  • 11
  • 2

1 Answers1

3

You are correct, GroupBox is not an ancestor - this is because the GridViewColumn is not added to the visual tree, so bindings that rely on visual tree navigation will not work. You could bind your columns to a static resource instead:

To achieve what you are after will require quite a bit of custom code. See teh following MSDN forum thread:

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/83bd7ab9-3407-461f-a0bc-69e04870075c

And the code here that gives much more options for specifying column sizes:

http://www.codeproject.com/KB/grid/ListView_layout_manager.aspx

ColinE
  • 68,894
  • 15
  • 164
  • 232