我想问问是否WPF具有任何功能,通过它可以在结合
当绑定做是为了类型的对象对象。我有类型的泛型集合对象
,必须处理为一个对象类型或其他(即的DateTime
, INT
等),在它的各种结合点。
I wanted to ask if WPF has any functionality by which one can define a target type on a binding
when the binding is done to an object of type "object
". I have a generic collection of type "object
" that must be handled as one object type or the other (ie. DateTime
, int
, etc.) at its various binding points.
有什么办法,我可以随意强制.net框架在编译时为 Type_X
的对象来对待未知类型的对象,对案件逐案基础处理绑定的时候?
Is there any way that I can arbitrarily force the .Net framework to treat objects of unknown type at compile time as objects of Type_X
, on a case-by-case basis when handling bindings?
没有,的 绑定
类不提供任何这样的功能;其实这并不需要,绑定框架源对象相关类型的自动转换,并且使用绑定的属性值,否则,如果未找到该属性引发一个绑定错误。
No, Binding
class doesn't provide any such feature; Actually it doesn't need to, Binding framework automatically converts the source object to relevant type and uses the bound property value, or else raises a binding error if that property is not found.
所以,如果我有对象
键入的属性我视图模型
So if I have a property of Object
type in my ViewModel
public object Dummy
{
get
{
return dummy;
}
set
{
dummy = value;
NotifyPropertyChanged("Dummy");
}
}
和我绑定到一个文本框
这样的 -
and I bound it to a TextBox
like this -
<TextBox Text="{Binding Dummy.Name}" HorizontalAlignment="Stretch"/>
和以后设置虚拟到这样一个对象 -
and later set the Dummy to an object like this -
Dummy = new MyCustomType();
在这一点上结合将评估并试图找到名称
源对象的属性(假
)和如果 MyCustomType
定义了一个名为公共财产名称
然后结合将执行否则会产生一个绑定错误(不例外只是一个错误味精,在输出窗口)。
at this point binding will evaluate and try to find the Name
property in the source object(Dummy
) and if MyCustomType
defines a public property named Name
then binding will execute else a binding error will occur( no exception just an error msg. in output window).
现在,如果我想我的假
属性设置为两种不同类型的对象(一个有名称
和其他具有名字
),那么我有两个选择 -
Now, in case I want to set my Dummy
property to objects of two different types(one having Name
and other having FirstName
) then I have two options -
的DataTemplates
作为Botz300建议
使用一个 ValueConverter
(其中检查对象的类型,并返回相应属性值)。
Define DataTemplates
as Botz300 suggested
Use a ValueConverter
(which checks the object type and returns relevant property value).
使用的另一种变体 ValueConverter
被定义的 类型转换器
做转换并声明它的 TypeConverterAttribute
,然后在绑定自动转换将完成。
Another variant of using ValueConverter
is defining a TypeConverter
to do conversion and declare it with the TypeConverterAttribute
, then in bindings automatic conversion will be done.
同样适用于收集对象。
更新:
是的,WPF将隐式使用 DefaultType的
转换器的类型转换为相应的显示值。如果您绑定的对象
直接属性(文本
在上面的例子中),那么WPF将使用类型转换器
为该类型(如果可用),或使用的ToString()
方法获取的显示值。
Yes, WPF will implicitly use DefaultType
converter to convert your type to relevant display value. In case you bind the Object
directly to a property(Text
in above example) then WPF will use the TypeConverter
for that type(if available) or use the ToString()
method to get the display value.
不过,这一切都取决于你所使用的显示数据,以及如何/你要绑定什么控制。
But all this depend on the control you are using for displaying the data and how/what you are binding.
请注意:这招只对足以能够有简单的对象 文本重新presentations。它可能没有意义的使用 在处理复杂的数据对象处理方便的技术。
Note: This trick only suffices for objects that can have simple textual representations. It might not make sense to use this convenient technique when dealing with complex data objects.
您可以通过这篇文章在MSDN上谈到了这一点 - 自定义数据显示与数据绑定和WPF
You can go through this article on MSDN which talks about this - Customize Data Display with Data Binding and WPF