Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
50
Binding expression errors when using AlternateBinding
posted

Hello, 

In our application we have a XamDataGrid with dynamic nested columns. We've set up the backing data classes as the following: 

    class Parent
    {
            public Parent(string name)
            {
                Name = name;
            }

            public string Name { get; set; }

            public List<Child> Children { get; set; } = new List<Child>();
    }

    class Child
    {
            public string Name { get; set; }
    }

and we setup the fields like the following:

            var unboundField = new Field()
            {
                     BindingType = BindingType.UseAlternateBinding,
                    AlternateBinding = new Binding()
                    {
                        Path = new PropertyPath("Parents[parentIndex].Children[{childIndex}].[{property.PropertyName}]")
                    }
            };

and then the data is created the following way:

        public VM()
        {
                var p1 = new Parent();
                var p2 = new Parent();

                var c1 = new Child();
                var c2 = new Child();

                var children = new List<Child>() { c1, c2 };

                p1.Children = children;

                Parents = new List<Parent>() { p1, p2 };
        }

I want to display a column for the parents name and then a column for each child object in the list of Parent.Children

The issue comes from the alternatebinding string because it's essentially hardcoded - if the children property of parent is an empty list (a valid use case) then trying to access the property of the child object causes an exception. Is there a way to check for the contents of the children list when fetching the value?

Parents
No Data
Reply
  • 34810
    Offline posted

    Hello Shael,

    Getting a binding expression error in this case is expected, as if the Parents or Children collection that you are alternate binding to is empty, this would internally cause an IndexOutOfRange exception for the values you are passing as parentIndex or childIndex.

    Being that you are creating a Field per item in this case, I would recommend that you check the item prior to creating the Field to ensure the Children collection is populated as this will avoid binding errors / exceptions.

Children