具有List <anotherobject> </anotherobject>属性的对象的分层数据模板

时间:2012-12-07 07:03:33

标签: c# wpf hierarchicaldatatemplate

我有一个具有这种结构的对象

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

我使用 ObjectToObservableListConverter

为Address,Parent,Children创建了一个分层模板
 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();
        }    

    }

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();
        }
    }

我创建的模板是:

 <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>

如果我在Parent上创建模板 并设置Children属性的ItemsSource路径,我没有获取Parent的剩余属性。 要么 如果我将ItemsSource设置为Parent类型的属性,并将user属性设置为list converter,则不会获得子级的层次结构。

我错过了什么?

请帮忙。在此先感谢

1 个答案:

答案 0 :(得分:1)

我找到了问题的解决方案。

我更改了转换器,将list属性设置为另一个自定义类,该类只包含列表类型的一个属性,并为其编写了一个分层数据模板。

<强> 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;
        }        
    }

分层数据模板

<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>
相关问题