Question

I have an object with this structure

public class Parent
{
  public string Name {get; set;}
  public int ID {get; set;}
  public List<Child> Children{set; get;}
  public Address HomeAddress {get; set;}
}

I created a hierarchical template for Address, Parent, Children with the ObjectToObservableListConverter

 public class ObjectToObservableListConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                return new ObservableCollection<Graphic> { (value as Graphic) };
            }
            return null;
        }


        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }    

    }

and PropertyToListConverter

public class PropertyToListConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Type type = value.GetType();
            PropertyInfo[] propertyList = value.GetType().GetProperties();
            List<object> values =
                (from property in propertyList
                 //where property.GetCustomAttributes(typeof(NotForTreeViewAttribute), false).Count() == 0
                 select property.GetValue(value, BindingFlags.Default, null, null, CultureInfo.InvariantCulture)).ToList();
            return values;

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

The templates I created are:

 <TreeView  ItemsSource="{Binding Path=Parent,
                                            Converter={StaticResource ResourceKey=objectToListConverter},
                                            UpdateSourceTrigger=PropertyChanged}">
            <TreeView.Resources>

    <HierarchicalDataTemplate DataType="{x:Type en:Parent}"
                                                  ItemsSource="{Binding Path=Children}">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Margin="0,4,0,0"
                                           VerticalAlignment="Center"
                                           FontWeight="Bold"
                                           Text="Children" />
                        </StackPanel>
                    </HierarchicalDataTemplate>
                    <HierarchicalDataTemplate DataType="{x:Type en:Child}"
                                                  ItemsSource="{Binding Converter={StaticResource ResourceKey=propertyToListConvertor}}">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Margin="0,4,0,0"
                                           VerticalAlignment="Center"
                                           FontWeight="Bold"
                                           Text="{Binding Path=ChildName}" />
                        </StackPanel>
                    </HierarchicalDataTemplate>
                    <HierarchicalDataTemplate DataType="{x:Type en:Address}"
                                                  ItemsSource="{Binding Converter={StaticResource ResourceKey=propertyToListConvertor}}">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Margin="0,4,0,0"
                                           VerticalAlignment="Center"
                                           FontWeight="Bold"
                                           Text="AddressType" />
                        </StackPanel>
                    </HierarchicalDataTemplate>

</TreeView.Resources>
</TreeView>

If I create the template on Parent and set the ItemsSource Path of the Children Property, I don't Get the remaining properties of Parent. Or If I set the ItemsSource as the property of type Parent and user property to list converter i dont get hierarchy of children.

What am I missing?

Please help. Thanks in Advance

Was it helpful?

Solution

I found the solution to the problem.

I changed my converter to set the list property to another custom class which contains only one property of list type and wrote a hierarchical data template for the same.

PropertyToListConverter

  public class PropertyToListConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                Type type = value.GetType();
                PropertyInfo[] propertyList = value.GetType().GetProperties();
                List<object> values =
                    (from property in propertyList
                     select GetValueOrElementName(value, property)).ToList();
                return values;

            }

            private static object GetValueOrElementName(object value, PropertyInfo property)
            {
                var propVal = property.GetValue(value, BindingFlags.Default, null, null, CultureInfo.InvariantCulture);

                if (property.PropertyType.IsGenericType && propVal != null)
                {
                    Type type = property.PropertyType.GetGenericArguments().FirstOrDefault();
                    if (type == typeof(Child))
                        return new EnumerableClass() { Children = propVal as List<Child> };                   
                }

                if (propVal != null && !string.IsNullOrEmpty(propVal.ToString()))
                    return propVal;


                return "Property["+ property.Name+"]";

            }


            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }

CustomClass

 public class EnumerableClass
    {        
        public string Name { get; set; }
        public List<Child> Children
        {
            get;
            set;
        }        
    }

Hierarchical Data template

<HierarchicalDataTemplate DataType="{x:Type en:Parent}"
                                              ItemsSource="{Binding Converter={StaticResource ResourceKey=propertyToListConvertor}}">
                    <StackPanel Orientation="Horizontal">                        
                        <TextBlock Margin="0,4,0,0"
                                       VerticalAlignment="Center"
                                       FontWeight="Bold"
                                       Text="{Binding Path=ParentName}" />
                    </StackPanel>
                </HierarchicalDataTemplate>
                <HierarchicalDataTemplate DataType="{x:Type ap:EnumerableClass}"
                                              ItemsSource="{Binding Path=Children,UpdateSourceTrigger=PropertyChanged}">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Margin="0,4,0,0"
                                       VerticalAlignment="Center"
                                       FontWeight="Bold"
                                       Text="Children" />
                    </StackPanel>
                </HierarchicalDataTemplate>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top