简体   繁体   English

如何在WPF中获取索引属性的PropertyType

[英]How to get the PropertyType of indexed property in WPF

I am working with DataBinding in DataGrid. 我正在使用DataGrid中的DataBinding。 I have a viewmodel which has a property called MyDataSource of type List. 我有一个视图模型,该模型具有名为MyDataSource的List类型的属性。 Class1 has a property called MyList of type List. Class1有一个名为MyList的属性,类型为List。 Class2 has a property called MyProperty of type string. Class2具有一个名为MyProperty的字符串类型的属性。

My DataGrid xaml looks like. 我的DataGrid xaml看起来像。

<DataGrid ItemsSource="{Binding MyDataSource}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="MyValue" Binding="{Binding Path=MyList[0].MyProperty}"/>
        </DataGrid.Columns>
</DataGrid>

Here I have access to PropertyPath MyList[0].MyProperty and MyDataSource in code. 在这里,我可以使用代码访问PropertyPath MyList [0] .MyProperty和MyDataSource。 Now I want to find out PropertyType for MyProperty by passing MyList[0].MyProperty in GetProperty method. 现在,我想通过在GetProperty方法中传递MyList [0] .MyProperty来查找MyProperty的PropertyType。

I followed the method described in the following link. 我遵循以下链接中描述的方法。 But here the PropertyInfo is null for MyList[0]. 但是,此处MyList [0]的PropertyInfo为null。 http://www.java2s.com/Code/CSharp/Reflection/Getsapropertysparentobject.htm http://www.java2s.com/Code/CSharp/Reflection/Getsapropertysparentobject.htm

Edited: 编辑:

I also tried the following code: 我还尝试了以下代码:

PropertyInfo pInfo = MyDataSource.GetType().GetProperty(MyList[0].MyProperty)

But pInfo returns null here. 但是pInfo在这里返回null。

Can anybody please suggest me a solution? 有人可以建议我解决方案吗?

Not sure what you want to achieve but you could use a ValueConverter like: 不确定要实现什么,但是可以使用ValueConverter,例如:

  class MyConverter : IValueConverter
  {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
      var typeName = ((Class2)value).GetType().GetProperty((string) parameter);
      return typeName.ToString();
    }

And XAML Binding: 和XAML绑定:

<Window x:Class="ThemeTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:conv="clr-namespace:ThemeTest"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <conv:MyConverter x:Key="propertyToParentTypeConverter"/>
    </Window.Resources>

... ...

    <DataGrid ItemsSource="{Binding MyDataSource}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="MyValue">
                <DataGridTextColumn.Binding>
                    <Binding Converter="{StaticResource propertyToParentTypeConverter}"  ConverterParameter="MyProperty" Path="MyList[0]" />
                </DataGridTextColumn.Binding>
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

If you need to traverse a chain of properties where one property is a collection then you can use reflection in the following way: 如果您需要遍历一系列属性,其中一个属性是一个集合,则可以通过以下方式使用反射:

  PropertyInfo pInfo = myObject.GetType().GetProperty("MyDataSource");
  if (pInfo != null && pInfo.PropertyType.FullName.StartsWith("System.Collections"))
  {
    foreach (object obj in ((IEnumerable)pInfo.GetValue(myObject, null)))
    {
      PropertyInfo pInfoElement = obj.GetType().GetProperty("MyList");
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM