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?