Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5934941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:11:04+00:00 2026-05-22T15:11:04+00:00

I have 3 datagrids that share the same data type. I’d like to configure

  • 0

I have 3 datagrids that share the same data type. I’d like to configure the column binding once and have the 3 datagrids share the resource.

e.g.

<DataGrid Grid.Row="1" x:Name="primaryDG" ItemsSource="{Binding Path=dgSource AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Width="Auto" Header="Column 1" Binding="{Binding Path=Col1}"/>
        <DataGridTextColumn Width="Auto" Header="Column 2" Binding="{Binding Path=Col2}"/>
        <DataGridTextColumn Width="Auto" Header="Column 3" Binding="{Binding Path=Col3}"/>
        <DataGridTextColumn Width="Auto" Header="Column 4" Binding="{Binding Path=Col4}"/>
    </DataGrid.Columns>
</DataGrid>

Is there a way to set the ItemsSource for each DataGrid, then use a datatemplate or controltemplate to get the columns?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:11:05+00:00Added an answer on May 22, 2026 at 3:11 pm

    Yes… two ways. One you can simply add a style for DataGrid that sets the columns like this…

    <Style x:Key="MyColumnDefsStyle" x:Shared="True" TargetType="DataGrid">
        <Setter Property="Columns">
            <Setter.Value>
                 <DataGridTextColumn Width="Auto" Header="Column 1" Binding="{Binding Path=Col1}"/>
                 <DataGridTextColumn Width="Auto" Header="Column 2" Binding="{Binding Path=Col2}"/>
                 <DataGridTextColumn Width="Auto" Header="Column 3" Binding="{Binding Path=Col3}"/>
                 <DataGridTextColumn Width="Auto" Header="Column 4" Binding="{Binding Path=Col4}"/>
            </Setter.Value>
        </Setter>
    </Style>
    
    <DataGrid Style="{StaticResource MyColumnDefsStyle}" ItemsSource="{Binding Foo1}" />
    <DataGrid Style="{StaticResource MyColumnDefsStyle}" ItemsSource="{Binding Foo2}" />
    <DataGrid Style="{StaticResource MyColumnDefsStyle}" ItemsSource="{Binding Foo3}" />
    

    That works but represents a problem if you are applying it to multiple grids that themselves may already be using a style.

    In that case, the other, more flexible way works better. This however requires creating a XAML-friendly classes to represent an ObservableCollection<DataGridColumn> (although you technically only said columns, I like to be complete myself so I’d also do one for the rows) Then add them in a place you can reference in the XAML namespaces. (I call mine xmlns:dge for ‘DataGridEnhancements’) You then use it like this:

    In the code somwhere (I’d make it accessible app-wide)…

    public class DataGridRowsCollection : ObservableCollection<DataGridRow>{}
    public class DataGridColumnsCollection : ObservableCollection<DataGridColumn>{}
    

    Then in the resources…

    <dge:DataGridColumnsCollection x:Key="MyColumnDefs" x:Shared="True">
        <DataGridTextColumn Width="Auto" Header="Column 1" Binding="{Binding Path=Col1}"/>
        <DataGridTextColumn Width="Auto" Header="Column 2" Binding="{Binding Path=Col2}"/>
        <DataGridTextColumn Width="Auto" Header="Column 3" Binding="{Binding Path=Col3}"/>
        <DataGridTextColumn Width="Auto" Header="Column 4" Binding="{Binding Path=Col4}"/>
    </dge:DataGridColumnsCollection>
    

    And finally in the XAML…

    <DataGrid Columns="{StaticResource MyColumnDefs}" ItemsSource="{Binding Foo1}" />
    <DataGrid Columns="{StaticResource MyColumnDefs}" ItemsSource="{Binding Foo2}" />
    <DataGrid Columns="{StaticResource MyColumnDefs}" ItemsSource="{Binding Foo3}" />
    

    HTH,

    Mark

    EDIT:
    Since you cannot set the DataGrid.Columns property, you need to enhance your DataGridView (as mentioned in the comments). Here is the code for an EnhancedDataGrid:

    public class EnhancedDataGrid : DataGrid
        {
            //the dependency property for 'setting' our columns
            public static DependencyProperty SetColumnsProperty = DependencyProperty.Register(
                "SetColumns",
                typeof (ObservableCollection<DataGridColumn>),
                typeof (EnhancedDataGrid),
                new FrameworkPropertyMetadata
                {
                    DefaultValue = new ObservableCollection<DataGridColumn>(),
                    PropertyChangedCallback = EnhancedDataGrid.SetColumnsChanged,
                    AffectsRender = true,
                    AffectsMeasure = true,
                    AffectsParentMeasure = true,
                    IsAnimationProhibited = true,
                    DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
                });
    
            //callback to reset the columns when our dependency property changes
            private static void SetColumnsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                var datagrid = (DataGrid) d;
    
                datagrid.Columns.Clear();
                foreach (var column in (ObservableCollection<DataGridColumn>)e.NewValue)
                {
                    datagrid.Columns.Add(column);
                }
            }
    
            //The dependency property wrapper (so that you can consume it inside your xaml)
            public ObservableCollection<DataGridColumn> SetColumns
            {
                get { return (ObservableCollection<DataGridColumn>) this.GetValue(EnhancedDataGrid.SetColumnsProperty); }
                set { this.SetValue(EnhancedDataGrid.SetColumnsProperty, value); }
            } 
        }
    

    Now you could set the columns with the SetColumns dependency property created in your CustomControl:

    <custom:EnhancedDataGrid SetColumns="{StaticResource MyColumnDefs}" ItemsSource="{Binding Foo1}" />
    <custom:EnhancedDataGrid SetColumns="{StaticResource MyColumnDefs}" ItemsSource="{Binding Foo2}" />
    <custom:EnhancedDataGrid SetColumns="{StaticResource MyColumnDefs}" ItemsSource="{Binding Foo3}" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with a data-bound DataGrid control, in that despite each column
I have a DataGrid that is setup like this: <DataGrid AutoGenerateColumns=True GridLinesVisibility=Horizontal IsReadOnly=True ItemsSource={Binding
I have a datagrid column that I am using an itemRenderer. Something like this
I have a web form that binds a DataGrid to a, normally, different data
I have an xml file providing data for a datagrid in Flex 2 that
I have an AIR application with two DataGrids that I would like to export
I have a Flex application with a couple of DataGrids with data. I'd like
I have a datagrid which lists products and their market share - (dgProd). That
I have a Datagrid that gets its data from an ArrayCollection of model beans.
I have a DataGrid and I want to populate a column that contains a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.