2
Vote

Unable to bind columns to child property of an object

description

The collection my DataGrid is bound to contains objects with child properties. Autofilter does not work for these child properties.
<DataGrid ItemsSource="{Binding Files}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding ID}" Header="Id" />
        <DataGridTextColumn Binding="{Binding Name}" Header="Name" />
        <DataGridTextColumn Binding="{Binding Details.Size}" Header="Size" />
    </DataGrid.Columns>
</DataGrid>
Image

When I use the following binding, the filter does work, but the grid shows an empty column because the binding is invalid:
<DataGridTextColumn Binding="{Binding Source=Details, Path=Size}" Header="Size" />
Do you know how to fix this issue?

file attachments

comments

BorisUK wrote Jan 27, 2015 at 1:50 PM

I have been able to solve the problem partially using bsFilter:ColumnFilter.BindingPath property:
                <DataGridTextColumn Binding="{Binding Details.Size}" Header="Size" bsFilter:ColumnFilter.BindingPath="Size" />
However, this does not seem to work when binding to Entity Framework objects.

Mishkin wrote Jan 28, 2015 at 11:57 AM

For complex binding, it is need to implement next (from v1.1.0.1):
  public class FileSizeFIM : BolapanControl.ItemsFilter.Initializer.FilterInitializersManager {
        public FileSizeFIM() {
            Add(new FileSizeValueFilterInitializer());
            // Add other filter initializers there...
        }
    }
    public class FileSizeValueFilterInitializer : BolapanControl.ItemsFilter.Initializer.FilterInitializer {
        public override Filter NewFilter(BolapanControl.ItemsFilter.FilterPresenter filterPresenter, object key) {
            if (key is string && (string)key == "Details.Size") {// from v1.1.0.1, key with "." pass to initializer
                return new FileSizeValueFilter();
            }
            return null;
        }
    }
    public class FileSizeValueFilter : RangeFilter<long> {
        public FileSizeValueFilter() : base(FileSizeGetter) { }
        private static long FileSizeGetter(object iFile) {
            return ((File)iFile).Details.Size; // Get file size from item of collection
        }
    }
...
<DataGridTextColumn Binding="{Binding Source=Details, Path=Size}" Header="Size" >
     <bsFilter:ColumnFilter.Initializers>
            <vw:FileSizeFIM />
      </bsFilter:ColumnFilter.Initializers>
</DataGridTextColumn>
See also binding to column "Supplier" in form ProductsView.xaml (v1.1.0.1) and file SupplierNameFIM.cs