DataGrid:同一列中的不同单元格类型 [英] DataGrid: Different Cell Types in Same Column

查看:78
本文介绍了DataGrid:同一列中的不同单元格类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ViewModel,其中包含以下属性(未显示任何与MVVM相关的代码):

I have a ViewModel that has among others the following properties (not showing any MVVM related code):

class MyVM {

    string Name {get;组; }¥b $ b   类型类型{get;组; }¥b $ b    object Value {get;组; } b $ b}

class MyVM {
   string Name { get; set; }
   Type Type { get; set; }
   object Value { get; set; }
}

基本上,它就像"属性"一样。 Visual Studio中的窗口。我有名称或属性,它可以接受的类型(int,float,string或list)和实际值。我想在DataGrid中显示它们。 Value列应该基于Type属性创建
单元格,即如果类型为字符串则为文本字段,如果类型为int则仅接受int的文本字段等。我只看到类型更改为的解决方案整个列,但不是逐个单元格。我该怎么做?

Basically, it's like the "Properties" window in Visual Studio. I have the name or the property, the type it can accept (either int, float, string or list) and the actual value. I want to show those in a DataGrid. The Value column should create cells based on the Type property, i.e. a text field if the type is string, a text field only accepting int if the type is int, etc. I only see solutions where the type is changed for an entire column but not on a cell-by-cell basis. How do I do this?

推荐答案



喜欢hulamula,



>>"值"列应根据"类型"属性创建单元格,即文本字段(如果类型为字符串,文本)如果类型为int,则仅接受int的字段等。我只看到解决方案,其中类型
针对整个列而不是逐个单元地更改。我该怎么做?




在你的MVVM模式中,你可以为单元格创建一个ViewModel并绑定它作为ItemSource。在您的ViewModel中,您可以设置所需的元素。  



然后,您可以使用
CellTemplateSelector
根据类型和值值为其他列中的单元格选择模板。 



如需更详细,请参阅:



WPF DataGrid,每个Cell中包含不同的UserControl ;



$

最好的问候,



Yong Lu



Hi hulamula,

>>The Value column should create cells based on the Type property, i.e. a text field if the type is string, a text field only accepting int if the type is int, etc. I only see solutions where the type is changed for an entire column but not on a cell-by-cell basis. How do I do this?

In your MVVM-Pattern, you can create a ViewModel for cells and bind it as ItemSource. In your ViewModel you can set the Elements that you need.  

Then, you can use the CellTemplateSelector to select a template for the cells in another column based on the Type and Value value. 

For more detailed, please see:

WPF DataGrid with different UserControl in each Cell;



Best Regards,

Yong Lu


这篇关于DataGrid:同一列中的不同单元格类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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