当前位置: 首页 > 编程语言 > C#

分享_1117

时间:2023-04-10 19:58:02 C#

使用复选框或文本框作为枚举类型使用复选框或文本框作为枚举类型如果我有这样的结构:publicclassParent{publicstringName{get;设置;}公共列表孩子{get;设置;}}公共类儿童{公共字符串名称{get;设置;}公共字符串值{get;设置;}公共枚举ValueType{get;设置;}}publicenumValueType{Int,Boolean,String};publicclassParentFactory{publicListParents{get;设置;}publicParentFactory(){Childchild1=newChild(){Name="Filter1",Value="1",ValueType=ValueType。细绳};孩子child2=newChild(){Name="isExecuted",Value="true",ValueType=ValueType.Boolean};Childchild3=newChild(){Name="Width",Value="10",ValueType=ValueType.Int};Parentparent1=newParent(){Name="Adam",Childs=newList(){child1,child2}};Parentparent2=newParent(){Name="Kevin",Childs=newList(){child3}};Parents=newList(){parent1,parent2};我想绑定对象:ParentFactoryparentFactory=newParentFactory()到ItemsControl:在Stackpanel中,有一个控件:TextBox但是,我希望它更动态:如果ValueType是布尔值,则使用复选框,否则使用文本框:。这可能吗?如果可以,我该如何实现?我认为最简单的方法是在面板中同时使用它们并定义两个新的布尔属性,这些属性将通过BooleanToVisibilityValueConverter控制它们的可见性。publicboolIsValueTypeBoolean{get{...将为CheckBox返回true的条件。}}publicboolIsValueTypeOther{get{return!this.IsValueBoolean;}}布尔值到VisibilityConverter[ValueConversion(typeof(bool),typeof(Visibility))]publicclassBooleanToVisibilityConverter:IValueConverter{publicobjectConvert(objectvalue,TypetargetType,objectparameter,System.Globalization.CultureInfoculture){boolmyValue=(布尔)值;如果(myValue)返回Visibility.Visible;elsereturn.Collapsibility}publicobjectConvertBack(objectvalue,TypetargetType,objectparameter,System.Globalization.CultureInfoculture){thrownewNotImplementedException();将此作为资源添加到您的XAML:我希望没有拼写错误...您可以动态更改DataTemplateXAML//Doyoubinding//DoyoubindingMakesureyousetBindingscorrectly...并为Condition1和Condition2创建DataTemplates希望对你有帮助:)以上就是C#学习教程:使用checkbox或者textbox作为枚举类型分享的所有内容,如果对你有用,需要进一步了解C#学习教程,希望大家多多关注对它—;本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处: