0

I'm creating a library that contains multiple usercontrols which I'm using in my applications. I'm currently struggling with the datagrid. I've created a custom control that contains a simple datagrid with some styling. I've created a dependency property which holds the columns the datagrid should show.

The dependency property looks like this:

public ObservableCollection<DataGridColumn> ColumnsSource
        {
            get { return (ObservableCollection<DataGridColumn>)GetValue(ColumnsSourceProperty); }
            set { SetValue(ColumnsSourceProperty, value); }
        }
        
        public static readonly DependencyProperty ColumnsSourceProperty =
            DependencyProperty.Register("ColumnsSource", typeof(ObservableCollection<DataGridColumn>), typeof(bDataGrid), new PropertyMetadata(new ObservableCollection<DataGridColumn>()));

I want to use the control like this in my application:

<custom:bDataGrid>
    <custom:bDataGrid.ColumnsSource>
       <DataGridTextColumn Header="First Column"/>
       <DataGridTextColumn Header="Second Column"/>
       <DataGridTextColumn Header="Third Column"/>
    </custom:bDataGrid.ColumnsSource>
</custom:bDataGrid>

I can't figure out how to use it that way. I want the datagrid in the usercontrol to take these values and create the corresponding columnheaders.

Any advise?

All columns that has been set through the usercontrol dependency property should show up in the datagrid as a column.

EDIT:

Solved it by reading the following link How do I bind a WPF DataGrid to a variable number of columns?

gore85
  • 9
  • 2

0 Answers0