动态绑定wpf

动态绑定wpf,wpf,winforms,binding,Wpf,Winforms,Binding,嗨,这是我的第一个问题:) 此示例在winform应用程序和wpf应用程序上进行了测试,并解决了wpf上绑定的问题 winform all在ICustomTypeDescriptor和grid draw only列添加到字典属性(Name Age)并排除Male的情况下运行良好 WPF绘制在网格上的类person的所有属性(姓名年龄男性) 关于这种情况或者wpf中ICustomTypeDescriptor的等效接口,您知道吗 <Grid> <DataGrid AutoGen

嗨,这是我的第一个问题:)

此示例在winform应用程序和wpf应用程序上进行了测试,并解决了wpf上绑定的问题

  • winform all在ICustomTypeDescriptor和grid draw only列添加到字典属性(Name Age)并排除Male的情况下运行良好
  • WPF绘制在网格上的类person的所有属性(姓名年龄男性)
关于这种情况或者wpf中ICustomTypeDescriptor的等效接口,您知道吗

<Grid>
<DataGrid AutoGenerateColumns="True" Height="200" HorizontalAlignment="Left" Margin="90,30,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="325" />
</Grid>


List persons=新列表();
新增(新人员(“Aymane”,30));
新增(新人员(“Raouia”,30));
grid.ItemsSource=人//wpf
grid.DataSource=人//框架

公共类人物:ICustomTypeDescriptor
{
字典属性=新字典();
公众人士()
{
添加(“名称”,空);
添加(“年龄”,空);
}
公共人物(字符串名称、对象值)
:base()
{
男=真;
名称=名称;
年龄=价值;
}
公共布尔男性{get;set;}
公共对象年龄{get{returnproperties[“Age”];}set{Properties[“Age”]=value;}
公共对象名称{get{return Properties[“Name”];}set{Properties[“Name”]=value;}
#区域ICustomTypeDescriptor成员
AttributeCollection ICustomTypeDescriptor.GetAttributes()
{
返回TypeDescriptor.GetAttributes(this,true);
}
字符串ICustomTypeDescriptor.GetClassName()
{
返回TypeDescriptor.GetClassName(此为true);
}
字符串ICustomTypeDescriptor.GetComponentName()
{
返回TypeDescriptor.GetComponentName(此为true);
}
TypeConverter ICustomTypeDescriptor.GetConverter()
{
返回TypeDescriptor.GetConverter(此为true);
}
EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
{
返回TypeDescriptor.GetDefaultEvent(this,true);
}
PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
{
返回TypeDescriptor.GetDefaultProperty(this,true);
}
对象ICustomTypeDescriptor.GetEditor(类型editorBaseType)
{
返回TypeDescriptor.GetEditor(this,editorBaseType,true);
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents(属性[]属性)
{
返回TypeDescriptor.GetEvents(属性,true);
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
{
返回((ICustomTypeDescriptor)this.GetEvents(null);
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(属性[]属性)
{
列表道具=新列表();
添加(新的PersonPropertyDescriptor(“名称”,属性));
添加(新的PersonPropertyDescriptor(“年龄”,属性));
返回新的PropertyDescriptorCollection(props.ToArray());
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
{
返回((ICustomTypeDescriptor)this.GetProperties(null);
}
对象ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
{
归还这个;
}
#端区
类PersonPropertyDescriptor:PropertyDescriptor
{
public PersonPropertyDescriptor(字符串名称,属性[]属性)
:base(名称、属性)
{
}
公共覆盖布尔CanResetValue(对象组件)
{
返回true;
}
公共重写类型ComponentType
{
获取{return typeof(Person);}
}
公共覆盖对象GetValue(对象组件)
{
返回((个人)组件)。属性[名称];
}
公共覆盖布尔为只读
{
获取{return false;}
}
公共覆盖类型PropertyType
{
获取{return typeof(object);}
}
公共替代无效重置值(对象组件)
{
((个人)组件)。属性[名称]=空;
}
公共覆盖无效设置值(对象组件、对象值)
{
((个人)组件)。属性[名称]=值;
}
公共重写bool ShouldSerializeValue(对象组件)
{
返回false;
}
}
}

要控制列生成,请处理
自动生成列
事件,您可以通过设置
e.Cancel=true来抑制列的生成

就你而言:

private void DataGridAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    var dataGrid = sender as DataGrid;
    if (dataGrid != null)
    {
        ICustomTypeDescriptor typeDescriptor =
            dataGrid.Items[0] as ICustomTypeDescriptor;
        if (typeDescriptor != null)
        {
            var props = typeDescriptor.GetProperties();

            if (!props.Contains((PropertyDescriptor)e.PropertyDescriptor))
            {
                e.Cancel = true;
            }
        }
    }
}
DataGrid定义为:

    <DataGrid
        AutoGenerateColumns="True"
        Height="311"
        HorizontalAlignment="Left"
        Name="dataGrid1"
        VerticalAlignment="Top"           
        Width="509"
        AutoGeneratingColumn="DataGridAutoGeneratingColumn">


给出所需的结果。

这里是ICustomTypeDescriptor和ITypedList的正确实现

namespace CustomTypeDescriptor
{

类行:ICustomTypeDescriptor{}
类RowsCollection:List,ITypedList{}
类表:IListSource、IEnumerable、IEnumerator
{
行集合行{get;set;}
}

}

多么性别歧视的数据结构……我需要自动生成列,这就是为什么我要使autogeneratecolumns=true
    <DataGrid
        AutoGenerateColumns="True"
        Height="311"
        HorizontalAlignment="Left"
        Name="dataGrid1"
        VerticalAlignment="Top"           
        Width="509"
        AutoGeneratingColumn="DataGridAutoGeneratingColumn">
namespace CustomTypeDescriptor
class Row : ICustomTypeDescriptor { }

class RowsCollection : List<Row>, ITypedList { }

class Table : IListSource, IEnumerable<Row>, IEnumerator<Row>
{
    RowsCollection Rows { get; set; }
}