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

  • Home
  • SEARCH
  • 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 5945215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:39:00+00:00 2026-05-22T16:39:00+00:00

The following contrived example illustrates my problem. My ViewModel: public class MyViewModel : ViewModel

  • 0

The following contrived example illustrates my problem.

My ViewModel:

public class MyViewModel : ViewModel 
{
    public MyViewModel() 
    {
        var table = new DataTable("MyDatatable");
        table.Columns.Add("Some Val", typeof(double));
        table.Columns.Add("Ref");

        var row = table.NewRow();
        row["Some Val"] = 3.14;
        row["Ref"] = new {Title = "My Title", Description = "My Description"};
        table.Rows.Add(row);

        MyDataView = table.DefaultView;
    }

    DataView MyDataView { get; set; }   
}

Now my problem lies in my Xaml code. I want one of my column headers to be “My Title” with the corresponding row value to be “My Description”…

My Xaml:

<DataGrid ItemsSource="MyDataView" AutoGenerateColumns="False">
  <DataGrid.Columns>
    <!--This column works fine. -->
    <DataGridTextColumn Header="Some Val" Binding="{Binding 'Some Val'}" Width="50"/>
    <!--This column doesn't work... I'm looking for something similar to this: -->
    <DataGridTextColumn Header="{Binding Path='Ref.Title'}" Binding="{Binding Path='Ref.Description'}"/>
  </DataGrid.Columns>
</DataGrid>

Does this make sense? The second column header should be “My Title” with the cell value being “My Description”. Any idea on how to do this?

  • 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-22T16:39:01+00:00Added an answer on May 22, 2026 at 4:39 pm

    I believe your second column should be:

    <DataGridTextColumn Header="{Binding Path=['Ref'].Title}"
        Binding="{Binding Path=['Ref'].Description}"/>
    

    EDIT:

    Ok, it looks like the DataGridTextColumn will not inherit the DataContext from the DataGrid. In fact, it’s not a FrameworkElement or Freezable so it won’t have a DataContext at all.

    The column should apply to any number of rows, even if you only have 1 row. The Header needs to be generic for all rows, while the Binding is row specific. But, you are trying to bind both to the first row.

    If you actually had two rows with different titles, then which should be displayed in the header?

    For Description, the following works:

    public class Test {
        public string Title { get; set; }
        public string Description { get; set; }
    }
    
    public class MyViewModel  {
        public MyViewModel() {
            var table = new DataTable("MyDatatable");
            table.Columns.Add("Some Val", typeof(double));
            table.Columns.Add("Ref", typeof(Test));
    
            var row = table.NewRow();
            row["Some Val"] = 3.14;
            row["Ref"] = new Test() { Title = "My Title", Description = "My Description" };
            table.Rows.Add(row);
    
            MyDataView = table.DefaultView;
        }
    
        public DataView MyDataView { get; set; }
    }
    

    With the XAML looking like:

    <DataGrid ItemsSource="{Binding MyDataView}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Some Val" Binding="{Binding 'Some Val'}" Width="50" />
            <DataGridTextColumn Header="Test" Binding="{Binding Path=[Ref].Description}" />
        </DataGrid.Columns>
    </DataGrid>
    

    If you remove the typeof(Test) part when defining the column, then Path=[Ref] will refer to a string, not an object of type Test. So I don’t think you’ll be able to use anonymous types.

    JP Richardson’s EDIT:

    Based upon CodeNaked’s answer, I was reminded of some previous problems that I knew solutions to, these allowed me to completely solve the problem.

    First, it is possible to bind to anonymous datatypes. The code modification should be be:

    table.Columns.Add("Ref", typeof(object));
    

    Instead of:

    table.Columns.Add("Ref");
    

    As CodeNaked stated, without the typeof(object) the default object type is assumed to be a string.

    Also, as CodeNaked stated, the DataGridTextColumn does not ‘know’ either DataContext for the row or the DataGrid. For every Window object (typicall MVVM scenario, there is just one) should have this code in place:

    private static void RegisterDataGridColumnsToDataContext() {
            FrameworkElement.DataContextProperty.AddOwner(typeof(DataGridColumn));
            FrameworkElement.DataContextProperty.OverrideMetadata(typeof(DataGrid), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits, new PropertyChangedCallback(OnDataContextChanged)));
    }
    
    public static void OnDataContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
            DataGrid grid = d as DataGrid;
            if (grid != null)
                foreach (DataGridColumn col in grid.Columns)
                    col.SetValue(FrameworkElement.DataContextProperty, e.NewValue);
    }
    

    And then call “RegisterDataGridColumnsToDataContext()” before the Window is shown.

    Modify MyViewModel to look like this:

    public class MyViewModel : ViewModelBase {
      ...
      public string ColumnTitle { get { return "My Title"; } }
      ...   
    }
    

    The modified Xaml would then look like this:

    <DataGrid ItemsSource="{Binding MyDataView}" AutoGenerateColumns="False">
      <DataGrid.Columns>
        <DataGridTextColumn Header="Some Val" Binding="{Binding 'Some Val'}" Width="50" />
        <DataGridTextColumn Header="{Binding (FrameworkElement.DataContext).ColumnTitle, RelativeSource={x:Static RelativeSource.Self}}" Binding="{Binding Path=[Ref].Description}" />
      </DataGrid.Columns>
    </DataGrid>
    

    Note: Originally the reason that I had the column header title in each row was that I couldn’t think of another way to bind it to the ‘Header’ property of my DataGridTextColumn. I was planning for the ‘Title’ property of each anonymous object to be the same. Fortunately the internet prevails.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following contrived example (coming from real code): template <class T> class
The following is a contrived example Makefile illustrating a problem that I'm having. release:
Let's say I have the following contrived example: var allListItems = $('li'); ... doSomeStuff();
My simplified and contrived example is the following:- Lets say that I want to
I have the following contrived example in Rails. I want to make sure the
The following query is a contrived example that demonstrates a bug I found in
I'd like to do the following in Oracle 10g (this is a contrived example
Given the following contrived example: I would like to query my data for all
Lets imagine I have something resembling the following contrived example ParkingSpace Car ParkingSpaceCar -------------
The following is a contrived example I just made up to help me understand

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.