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 3280038
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:39:35+00:00 2026-05-17T19:39:35+00:00

I have a File class defined as following public class File { public string

  • 0

I have a File class defined as following

public class File
{
 public string FileName {set;get;}
 public List<Property> PropertyList;
}

Here is how Property class looks like:

public class Property
{
 public string PropertyName { set; get;};
 public string PropertyValue { set; get;};
 ...
}

I need to bind a List<File> to a DataGrid, display FileName. Also, I want to create a column for each Property in the PropertyList,
with the string value of PropertyName as the column title, and string value of PropertyValue as the value of the column.

Is this possible in WPF?

Thanks,

  • 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-17T19:39:36+00:00Added an answer on May 17, 2026 at 7:39 pm

    Just had to try this one, odd but funny problem:-) Managed to get it working by using the following.
    Sorry for the long answer, probably way to detailed 🙂

    First of, the DataGrid. Plain and simple

    <DataGrid Name="c_dataGrid" AutoGenerateColumns="False"
              CanUserAddRows="False" CanUserDeleteRows="False"/>
    

    Then the File class, named MyFile

    public class MyFile
    {
        public MyFile() : this(string.Empty) {}
        public MyFile(string fileName)
        {
            FileName = fileName;
            MyPropertyList = new ObservableCollection<MyProperty>();
        }
    
        public string FileName
        {
            set;
            get;
        }
        public ObservableCollection<MyProperty> MyPropertyList
        {
            get;
            set;
        }
    }
    

    Property class, named MyProperty

    public class MyProperty
    {
        public MyProperty() : this(string.Empty, string.Empty) {}
        public MyProperty(string propertyName, string propertyValue)
        {
            MyPropertyName = propertyName;
            MyPropertyValue = propertyValue;
        }
        public string MyPropertyName
        {
            set;
            get;
        }
        public string MyPropertyValue
        {
            set;
            get;
        }
    }
    

    A list containing MyFiles

    public ObservableCollection<MyFile> MyFileList{ get; set; }
    

    Created some dummy data to populate the list and set the ItemsSource on the DataGrid

    MyFile myFile1 = new MyFile("MyFile1");
    myFile1.MyPropertyList.Add(new MyProperty("Name1", "Value1"));
    myFile1.MyPropertyList.Add(new MyProperty("Name2", "Value2"));
    myFile1.MyPropertyList.Add(new MyProperty("Name3", "Value3"));
    MyFile myFile2 = new MyFile("MyFile2");
    myFile2.MyPropertyList.Add(new MyProperty("Name1", "Value1"));
    myFile2.MyPropertyList.Add(new MyProperty("Name4", "Value4"));
    myFile2.MyPropertyList.Add(new MyProperty("Name5", "Value5"));
    MyFileList = new ObservableCollection<MyFile>();
    MyFileList.Add(myFile1);
    MyFileList.Add(myFile2);
    c_dataGrid.ItemsSource = MyFileList;
    

    Added a DataGridTextColumn for the FileName attribute

    c_dataGrid.Columns.Add(GetNewMyFileNameColumn());
    
    private DataGridColumn GetNewMyFileNameColumn()
    {
        DataGridTextColumn myFileNameColumn = new DataGridTextColumn();
        myFileNameColumn.Header = "FileName";
        myFileNameColumn.Width = new DataGridLength(1.0, DataGridLengthUnitType.Auto);
    
        Binding valueBinding = new Binding();
        valueBinding.Path = new PropertyPath("FileName");
        valueBinding.Mode = BindingMode.TwoWay;
        valueBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        valueBinding.NotifyOnSourceUpdated = true;
        valueBinding.NotifyOnTargetUpdated = true;
    
        myFileNameColumn.Binding = valueBinding;
    
        return myFileNameColumn;
    }
    

    And then for the MyPropertyList. I only wanted to add each MyPropertyName once so if I have the following
    MyFile1
    -Name1
    -Name2
    -Name3
    MyFile2
    -Name1
    -Name4
    -Name5
    the generated columns shall be Name1, Name2, Name3, Name4 and Name5.

    foreach (MyFile myFile in MyFileList)
    {
        foreach (MyProperty myProperty in myFile.MyPropertyList)
        {
            if (ColumnAlreadyAdded(myProperty.MyPropertyName) == false)
            {
                c_dataGrid.Columns.Add(GetNewMyPropertyColumn(myProperty.MyPropertyName));
            }
        }
    }
    
    private bool ColumnAlreadyAdded(string myPropertyName)
    {
        foreach (DataGridColumn dataGridColumn in c_dataGrid.Columns)
        {
            if (dataGridColumn.Header.ToString() == myPropertyName)
            {
                return true;
            }
        }
        return false;
    }
    
    private DataGridColumn GetNewMyPropertyColumn(string myPropertyName)
    {
        DataGridTextColumn myFileNameColumn = new DataGridTextColumn();
        myFileNameColumn.Header = myPropertyName;
        myFileNameColumn.Width = new DataGridLength(1.0, DataGridLengthUnitType.Auto);
    
        Binding valueBinding = new Binding();
        valueBinding.Path = new PropertyPath("MyPropertyList");
        valueBinding.Converter = new MyPropertyConverter(myPropertyName);
        valueBinding.Mode = BindingMode.TwoWay;
        valueBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        valueBinding.NotifyOnSourceUpdated = true;
        valueBinding.NotifyOnTargetUpdated = true;
    
        myFileNameColumn.Binding = valueBinding;
    
        return myFileNameColumn;
    }
    

    I had to feed the MyPropertyName to the constructor of the Converter so it would know which property to look for.
    And finally the Converter

    public class MyPropertyConverter : IValueConverter
    {
        private string m_propertyName = string.Empty;
        ObservableCollection<MyProperty> m_myPropertyList = null;
    
        public MyPropertyConverter(string propertyName)
        {
            m_propertyName = propertyName;
        }
    
        object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            m_myPropertyList = value as ObservableCollection<MyProperty>;
            if (m_myPropertyList == null)
            {
                return null;
            }
            foreach (MyProperty myProperty in m_myPropertyList)
            {
                if (myProperty.MyPropertyName == m_propertyName)
                {
                    return myProperty.MyPropertyValue;
                }
            }
            return null;
        }
    
        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (m_myPropertyList != null)
            {
                foreach (MyProperty myProperty in m_myPropertyList)
                {
                    if (myProperty.MyPropertyName == m_propertyName)
                    {
                        myProperty.MyPropertyValue = value.ToString();
                        break;
                    }
                }
            }
            return m_myPropertyList;
        }
    }
    

    In Convert it will check for the given MyPropertyName and if it finds it, return the MyPropertyValue, otherwise null. The same goes for the ConvertBack method but it will set MyPropertyValue to the new value for the MyProperty with the given MyPropertyName and then return the list which is either a list or null.

    The properties that aren’t in a MyFile list won’t be editable, they will just change back to null upon leaving the cell (which is probably the point).

    This will result in a DataGrid that looks like this.

    alt text

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

Sidebar

Related Questions

If I have the core of a class defined in one file as public
I have a Java file TestThis.java like the following: class A { public void
I have a KMainWindow: //file.h class MainWindow: public KMainWindow { public: MainWindow(QWidget *parent =
I have the following problem using template instantiation [*]. file foo.h class Foo {
I have a Repository Class with the following method... public T Single<T>(Predicate<T> expression) {
Just imagine you have the following class [DataContract] public class NamedList { [DataMember] public
I have a controller class as in the following code segment: @Controller public class
I have an actionscript file that defines a class that I would like to
I have a class file that contains all the classes that are needed for
If I have a Java source file (*.java) or a class file (*.class), how

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.