1

I am trying to reuse the XAML that I am writing as much as possible.

Now, I would like to display my objects in DataGrids. My objects share some common properties through heritage. Therefore I would like to create a UserControl with a grid and among others define some columns for the properties that are common to each objects. So something looking like that:

<UserControl Name="MyCustomGrid">
    <Grid>
        <DataGrid>
            <!-- Common Settings -->
                ...
            <!-- /Common Settings -->
            <DataGrid.Columns>
                <ADataGridColumn Name="CommonProperty1"/>   
                <ADataGridColumn Name="CommonProperty2"/>
                ...
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</UserControl>

Then I would like to be able to use this UserControl and add extra columns for the specific properties of the objects I want to display, through XAML like so:

<view:myCustomGrid>
   <view:myCustomGrid.Columns>
       <ADataGridColumn Name="SpecificProperty1"/>
       <ADataGridColumn Name="SpecificProperty2"/>
       ...
   </view:myCustomGrid.Columns>
</view:myCustomGrid>

Is this possible ? I have looked at this question but it doesn't seem like I can insert an ContentControl under DataGrid.Columns

Maybe this is not the way to go...

Thanks for your help

beetix
  • 99
  • 9
  • showing simple additional columns may be easy but later on you probably need ranges, constraints and validations and that will be endless work. – Lei Yang Jul 13 '17 at 13:38

1 Answers1

0

You can define columns like this; including different types

<DataGrid>
    <DataGrid.Columns>
        <DataGridTemplateColumn/>
        <DataGridCheckBoxColumn/>
        <DataGridComboBoxColumn/>
        <DataGridHyperlinkColumn/>
        <DataGridTextColumn/>
    </DataGrid.Columns>
</DataGrid>

When you set AutoGenerateColumns="True" which is the default you will get the columns from your bound properties as well. If you only want to see the specified columns you have to set AutoGenerateColumns="False".

The DataGridTemplateColumn is for userspecific columns. So you can put everything you want in it.

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <!--Content-->
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <!--Content-->
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

But why do you want to encapsulate this in a UserControl instead of doing it for the DataGrid instances itself? I would suggest specify the columns for each DataGrid because it would be a a lot of work putting this into a UserControl. Or using styles when you have some things which are the same for each DataGrid.

Mighty Badaboom
  • 6,067
  • 5
  • 34
  • 51
  • i think the OP's concern is 'add columns at runtime', and currently he does not care about column types. – Lei Yang Jul 13 '17 at 13:57
  • @mighty-badaboom I know about the different column types. The thing is that I have 3 grids quite similar and that I have to make the modifications 3 times on common settings (and not just style). Auto generating doesn't seem ideal because I have custom columns and I want my columns in a certain order. What I had in mind doesn't seem possible just like that. I will use templates as much as I can, so that the DataGrids' XAML stays, as concise as possible. Thanks – beetix Jul 17 '17 at 08:12