【问题标题】:Selecting Controls based on Enum基于枚举选择控件
【发布时间】:2012-01-30 16:17:37
【问题描述】:

我有一个枚举,它基本上列出了要显示的控件类型,我想根据我的 ViewModel 中这个枚举的值,显示适当的控件。

类似这样的:

public enum ControlType
{
    TextBox,
    Date,
    ComboBox
}

public class ViewModel
{
    public ControlType ControlToDisplay { get; set; }
}

ControlToDisplay 的值在 ViewModel 中设置。在 XAML 中我需要使用这个属性来显示相应的控件。

是否有可能做到这一点,以及如何做到这一点?

谢谢!

【问题讨论】:

  • 看看DataTemplateSelector,它可能会做你想做的事(抱歉没有时间给出完整的答案,但有人可能会扩展)。
  • 这些 ViewModel 是否属于某种机会列表?
  • ViewModel 中有一个对象列表,每个对象都有一个 ControToDisplay 属性。

标签: .net wpf


【解决方案1】:

this post 中描述的DataTemplateSelector 效果很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多