akgulebubekir / Xamarin.Forms.DataGrid

DataGrid Component For Xamarin.Forms Projects
MIT License
259 stars 76 forks source link

DataGridColumn width property not working correctly when using converter #130

Closed gdzmt closed 4 years ago

gdzmt commented 4 years ago

Hi, I'm trying to "hide" a column based on a bool property in my View Model, I want to accomplish this by setting DataGridColumn width property to "0" when hidden (false) and "2*" when visible (true). I tried hard coding those values and it worked, but when using the binding converter, it only applies the changes to the data rows, not the header row.

<dg:DataGrid.Columns>
    <dg:DataGridColumn x:Name="Description" Title="Description" PropertyName="Description" Width="8*" />
    <dg:DataGridColumn x:Name="Quantity" Title="Quantity" PropertyName="Quantity" Width="2*"/>
    <dg:DataGridColumn x:Name="Time" Title="Time" PropertyName="Time" Width="{Binding IsTimeVisible, Converter={StaticResource BooleanConverter}}"/>
</dg:DataGrid.Columns>

Of course my converter is well imported as I'm able to debug it, here is the code:

public class ColumnWidthBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (targetType == typeof(double))
        {
            return (bool) value ? 150 : 0;
        }
        return (bool) value ? new GridLength(2, GridUnitType.Star) : new GridLength(0, GridUnitType.Absolute);
}

I tried to consider the case where the Header row asks for Double instead of GridLength as a value type, but haven't succeeded.

When trying to set the width programatically for a third hidden column, I end up with 3 header columns and 2 data columns.

akgulebubekir commented 4 years ago

Header creation event tirggered in 2 occurences: on Columns Property Change and OnParentSet method. I suggest that binding the columns. if you want to change the number of columns.