获取绑定到列的对象字段的数据类型 [英] get datatype of the object-field bound to a column

查看:123
本文介绍了获取绑定到列的对象字段的数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我只是想使用wpf工具包中的datagrid为我的应用程序构建一些动态过滤器.

我有一个具有不同数据类型(字符串,日期时间,整数)的几个字段的自定义对象.自定义对象位于observablecollection中,并通过collectionview绑定到datagrid.itemssource属性.

datagrid现在具有几个自定义列,这些列由到字段的路径绑定.

我如何获得对对象的数据类型(SORRY ..我的意思是FIELD!)的访问权限,即通过引用该列,而又不知道我使用的是什么确切的对象(想要保持其动态性),将其绑定到特定的列以便与任何"自定义对象一起使用.)

hello, i m just trying to build some dynamic filters for my application using the datagrid in the wpf toolkit.

i have a custom object with several fields of different datatypes (strings, datetimes, integers). the custom objects are in a observablecollection and are bound through a collectionview to the datagrid.itemssource property.

the datagrid now has severals custom columns which are bound by path to the fields.

how do i gain access to the datatype of the object (SORRY .. i meant FIELD!) thats is bound to a specific column by having a reference of the column, and without knowing what exact object i use (want to keep it dynamic in order to use it with "any" custom object).

推荐答案

我相信您可以使用反射.如果您有可能的类数据类型列表,则可以使用"as"关键字检查特定类型.
I believe you can use reflection. If you have a list of possible data types that are classes, you can use the ''as'' keyword to check for specific types.


仅在实际使用时才需要数据类型需要将对象强制转换为该数据类型,这意味着您必须事先为其编写代码.在这种情况下,可以使用"is"关键字来检查列的DataContext的数据类型.您也可以调用GetType()方法(从类对象派生的所有对象都具有此方法).使用"is"将使您的代码具有强类型,而使用GetType()将允许您使用switch语句根据数据类型确定逻辑流(如果您有一个大量的数据类型.)
You will only need the data type if you actually need to cast an object to that data type, which means you''ll have to code for it in advance. In that case, you can use the "is" keyword to check the data type of the DataContext of the column. You can also call the GetType() method (all objects that derive from class object, which all objects do, have this method). Using "is" will make your code strongly typed and using GetType() would allow you to use a switch statement to determine logic flow based on data type (that would make your code faster than using a chained "if" statement if you have a large number of data types).


使用GetType()将允许您使用switch语句根据数据类型确定逻辑流(这将使您的代码比使用链接的代码更快.如果有大量数据类型,则使用"if"语句."

我不相信这是真的.我相信内部的switch语句不会比大量的if/else statemetns快.它确实使您的代码更具可读性,这是一个更好的理由.

是的,我还要说的是,如果您不打算编写一些代码来直接处理该类型,为什么还需要知道该类型?
" using GetType() would allow you to use a switch statement to determine logic flow based on data type (that would make your code faster than using a chained "if" statement if you have a large number of data types)."

I don''t believe this is true. I believe that a switch statement internally is no faster than a ton of if/else statemetns. It DOES make your code more readable, which is a far better reason to do it.

And yes, I was also going to say, why do you need to know the type if you''re not going to write some code to deal with that type directly ?


这篇关于获取绑定到列的对象字段的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆