控件类型取决于数据类型或属性? [英] Control type depends on data type or property?

查看:112
本文介绍了控件类型取决于数据类型或属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速提问:

使用XAML进行简短工作,使基础数据配置/控制控件类型。我看到很多基于数据内容配置控件外观的例子;我希望将控件类型基于相关的数据/属性。



示例包括:

。值返回Bool对象,控件应该是一个按钮

。值返回Int32对象,控件应该是一个文本框



。数据可能会暴露一个属性,Widget,它会指定按钮,文本框等。



我想人们可以很容易地在C#中写这个或者拼凑一个包含的ContentControl所有可能的按钮,文本框等组合,数据可以控制哪些是可见的,但这些似乎是这样的黑客。我想我会问专家是否存在更自然的xaml解决方案。

解决方案

根据具体情况,一种可能性是建立一个自定义控件来处理''个人控制''。此自定义控件将适应数据类型等。



另一种选择可能是使用属性网格,例如 WPF工具包 - PropertyGrid的 [ ^ ]或例如 Native WPF 4 PropertyGrid [ ^ ]


如果PropertyGrid不是你想要的那么我想你正在寻找类似基于WPF的动态DataTemplateSelector [ ^ ]



祝你好运

Espen Harlinn

Quick question:
Is is short work using XAML to have the types of controls configured/controlled by the underlying data. I see plenty of examples of configuration of a control''s look/feel based on data content; I am looking to base the control type on the associated data/properties.

Examples include:
. Value returns Bool object, the control should be a button
. Value returns Int32 object, the control should be a textbox
or
. The data could expose a property, Widget, that would specify button, textbox, etc

I suppose one could write this in C# pretty readily or cobble together a ContentControl that contains all possible button, textbox, etc combination and the data could control which are visible but these seem such hacks. I thought I would ask the experts if a more natural xaml solution exists.

解决方案

Depending on the situation, one possibility could be to build a custom control that handles ''individual controls''. This custom control would adapt to the data type etc.

Another option could be to use a property grid such as Wpf Toolkit - PropertyGrid[^] or for example Native WPF 4 PropertyGrid[^]


If the PropertyGrid isn''t what you''re looking for then I guess you''re looking for something like WPF Based Dynamic DataTemplateSelector[^]

Best regards
Espen Harlinn


这篇关于控件类型取决于数据类型或属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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