0

I have a pretty large object and some of it's properties should be shown in a datagrid. The problem is that the binding is not doing well and the nested object values are empty.

The MainCustomObject is:

public class MainCustomObject
{
    public int Id { get; set; }
    public string Name { get; set; }
    public CustomObject1 CustomObject1 { get; set; }
}

The CustomObject1 is:

public class CustomObject1
{
    public string Name { get; set; }
    public string Url { get; set; }
}

And my XAML code is:

<DataGrid ItemsSource="{Binding MainCustomObjectList}" AutoGenerateColumns="False">
<DataGrid.Columns>
    <DataGridTextColumn Header="Name" Width="75" Binding="{Binding Name}" />
    <DataGridTextColumn Header="CustomObject1_Name" Width="75" Binding="{Binding CustomObject1.Name}" />
    <DataGridTextColumn Header="CustomObject1_Url" Width="75" Binding="{Binding CustomObject1.Url}" />
</DataGrid.Columns>
</DataGrid>

Thanks for the help guys.

13
  • is not doing well what? I mean: what's wrong with it? Commented Sep 18, 2018 at 11:48
  • 2
    The ItemsSource must be an IEnumerable. What is "ObjectList"? Commented Sep 18, 2018 at 11:49
  • 1
    And how does CustomObject1 look like? Does it have public Id and Name properties? Commented Sep 18, 2018 at 11:52
  • @Stefan I mean it does not show the values! It's all empty Commented Sep 18, 2018 at 14:17
  • @mm8 That's just a name for the IEnumerable. Changed it to prevent the misunderstanding. Commented Sep 18, 2018 at 14:18

2 Answers 2

1

I tested you code and it works in that way, that a row is created and displayed. Your Namespace or class name might be different.

MainWindow

public partial class MainWindow : Window
{
    public IEnumerable<MainCustomObject> MainCustomObjectList { get; set; } = new List<MainCustomObject>
    {
        new MainCustomObject
        {
            Id = 1,
            Name = "Name1",
            CustomObject1 = new CustomObject1
            {
                Name = "Custom name 1",
                Url = "Url 1"
            }
        }
    };
    public MainWindow()
    {
        InitializeComponent();
    }
}

XAML

<Window x:Class="WpfApp2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp2"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800" DataContext="{Binding RelativeSource={RelativeSource Self}}">
<StackPanel>
    <DataGrid ItemsSource="{Binding MainCustomObjectList}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
            <DataGridTextColumn Header="CustomObject1_Name" Binding="{Binding CustomObject1.Name}" />
            <DataGridTextColumn Header="CustomObject1_Url" Binding="{Binding CustomObject1.Url}" />
        </DataGrid.Columns>
    </DataGrid>
</StackPanel>
</Window>

Note that I bound the DataContext to my own instance. Otherwise it would show the same error you are reporting, an empty list. You also want to make sure, that the list is completely initialized, as mm8 suspects. If only the first column is filled, that is a hint, that the property CustomObject1 is null.

Sign up to request clarification or add additional context in comments.

Comments

0

You can only bind the ItemsSource property to an IEnumerable (including any type that implements the IEnumerable interface). So your XAML markup should work provided that MainCustomObjectList returns an IEnumerable<MainCustomObject> and that the CustomObject1 type has public Name and Url properties that are actually populated with some values.

Also make sure that DataContext of the DataGrid is an instance of the class where the MainCustomObjectList property is defined.

7 Comments

@Stefan: IEnumerable is an interface that you implement. All classes that implement this interface are indeed IEnumerables.
However, this fastest-gun-in-the-west style post does not at all answer the question, because you couldn't wait until OP clarified the questions in the comments.
@Clemens: If it doesn't answer the question, what is the question then?
Now that you've read all the comments on the question, you know that it is at least nothing of what you are mentioning here. Everything in your answer is just a guess, that turned out to be not the source of the problem. It still gave you two upvotes, which is all you want. But you've asked the right question in your last comment.
I edited my answer slightly to add that the properties must be set in order for any values to show up in the DataGrid.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.