根据属性的类型使用文本框或复选框

如果我有这个结构:

public class Parent { public string Name{get; set;} public List Childs {get; set;} } public class Child { public string Name{get; set;} public int Age {get; set;} public bool Married {get; set;} } public class ParentFactory { public List Parents {get; set;} public ParentFactory() { Child child1 = new Child() {Name="Peter", Age=10, Married=true}; Child child2 = new Child() {Name="Mary", Age=9, Married=false}; Child child3 = new Child() {Name="Becky", Age=12, Married=true}; Parent parent1 = new Parent(){Name="Adam", Childs = new List(){child1, child2}}; Parent parent2 = new Parent(){Name="Kevin", Childs = new List(){child3}}; Parents = new List(){parent1, parent2}; } } 

我想将对象ParentFactory parentFactory = new ParentFactory()绑定到ItemsControl:

                     

在Stackpanel中,有两种类型的控件:TextBox和CheckBox。 但是,我希望它们更具动态性:如果值是布尔值,则使用复选框,否则使用文本框。 这意味着我不需要在StackPanel中定义TextBox或Checkbox控件,因为我的Child类中有许多属性。 是否有可能,如果是的话,我怎样才能实现它们?

我从你的问题中理解我做了一个解决方案。 请看一下。 该示例基于DataTrigger,您可以将逻辑更改为Converter。

                                   

您可以动态更改DataTemplate

XAML

              

确保正确设置Bindings …并为Condition1Condition2创建DataTemplates

希望能帮助到你 :)

看看这篇文章: http : //www.drwpf.com/blog/Home/tabid/36/EntryID/24/Default.aspx我认为这就是你需要的:

        30 12/16/1970 True False Foo