w-ahmad / WinUI.TableView

A light weight TableView component for WinUI3
MIT License
136 stars 9 forks source link

Empty PropertyPath doesn't work in columns #42

Open dotMorten opened 2 months ago

dotMorten commented 2 months ago

Description:

Can't bind to the object itself using an empty or null property path.

Steps to Reproduce:

Create a column like this:

TableView.Columns.Add(new TableViewTextColumn() { Header = "Header 1", Binding = new Binding() { Path = new PropertyPath(""), Converter = converter, ConverterParameter = "fieldName" } });
TableView.Columns.Add(new TableViewTextColumn() { Header = "Header 2", Binding = new Binding() { Path = new PropertyPath(), Converter = converter, ConverterParameter = "fieldName2" } });

Throws:

System.NullReferenceException: 'Object reference not set to an instance of an object.'
This exception was originally thrown at this call stack:
    WinUI.TableView.TableViewColumnHeader.OnApplyTemplate()
    Microsoft.UI.Xaml.FrameworkElement.Microsoft.UI.Xaml.IFrameworkElementOverrides.OnApplyTemplate()
    ABI.Microsoft.UI.Xaml.IFrameworkElementOverrides.Do_Abi_OnApplyTemplate_2(nint)
    WinRT.ExceptionHelpers.ThrowExceptionForHR.__Throw|38_0(int)
    ABI.Microsoft.UI.Xaml.IUIElementMethods.Measure(WinRT.IObjectReference, Windows.Foundation.Size)
    Microsoft.UI.Xaml.UIElement.Measure(Windows.Foundation.Size)
    WinUI.TableView.TableViewHeaderRow.CalculateHeaderWidths.AnonymousMethod__4(WinUI.TableView.TableViewColumn)
    System.Linq.Enumerable.SelectListIterator<TSource, TResult>.MoveNext()
    System.Linq.Enumerable.Sum<TSource, TResult>(System.Collections.Generic.IEnumerable<TSource>)
    WinUI.TableView.TableViewHeaderRow.CalculateHeaderWidths()
    ...
    [Call Stack Truncated]

Expected behavior:

Binds to the entire row element, so it can be passed on to the converter.

Screenshots:

If applicable, add screenshots to help explain your problem.

Environment:

w-ahmad commented 2 months ago

Thank you, @dotMorten, for your feedback. While a null check can resolve this issue, a crash may occur if sorting or filtering is applied to the column. Currently, the sorting and filtering do not account for value converters from the binding. I'm considering some adjustments in the sorting and filtering logic to incorporate values converted by the converters before use. Hopefully this will be available in the next release.