簡體   English   中英

具有列表屬性的對象的分層數據模板<AnotherObject>

[英]Hierarchical Data Template of an object with a property as List<AnotherObject>

我有一個具有這種結構的物體

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為地址,父級,子級創建了分層模板

 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>

如果我在父級上創建模板並設置Children屬性的ItemsSource路徑,則不會獲得父級的其余屬性。 或者,如果我將ItemsSource設置為類型Parent的屬性和用戶屬性以列出轉換器,則我不會獲得子級層次結構。

我想念什么?

請幫忙。 提前致謝

我找到了解決問題的辦法。

我更改了轉換器,將list屬性設置為另一個自定義類,該類僅包含一個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();
            }
        }

自定義類

 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM