devexpress-wpfdevexpress-gridcontrol

How to show all inherited interfaces in GridControl


We have a GridControl and we are assigning the ItemsSource to a collection of interface items. The interface used in the collection inherits from another interface, and the problem we are running into is that only items directly defined in the top level interface are being shown in the GridControl

Below is a very simplified example of the behavior that we are seeing.

xaml code defining the GridControl

<Window x:Class="WpfThrowaway.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfThrowaway"
        xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <dxg:GridControl x:Name="DataGrid" Background="#363636" Foreground="#FFFFFF" EnableSmartColumnsGeneration="True" AutoGenerateColumns="AddNew">
            <dxg:GridControl.View >
                <dxg:TableView x:Name="TableView" AllowEditing="False" />
            </dxg:GridControl.View>
        </dxg:GridControl>
    </Grid>
</Window>

Concrete item implementation

    class ConcreteItem : ILevel1
    {
        public string Level1String => "Level 1 string";

        public double Level2Double => 2;
    }

Level 1 interface (type used in the ItemsSource collection)

    interface ILevel1 : ILevel2
    {
        string Level1String { get; }
    }

Level 2 interface

    interface ILevel2
    {
        double Level2Double { get; }
    }

Code behind to initialize ItemsSource in the MainWindow

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var concreteItemCollection = new List<ILevel1>();
            for(var i = 0; i < 100; i++)
            {
                concreteItemCollection.Add(new ConcreteItem());
            }
            DataGrid.ItemsSource = concreteItemCollection;
        }
    }

Resulting data grid

Grid Control showing only Level 1 column

What we want and would expect is for the GridControl to show two columns Level1String and Level2Double, but only the item explicitly defined in the ILevel1 interface is showing up in the grid.

Is there any workaround for this? How do we get all properties from inherited interfaces to show up as well?


Solution

  • A bit of a hack that works is to cast the top level interface to an object. It will trick the grid control into auto generating the columns based on the concrete implementation, which will get you all of your properties.

    DataGrid.ItemsSource = concreteItemCollection.Select(x => (object)x);