问题描述
如果我有这样的结构:
public class Parent
{
public string Name{get; set;}
public List<Child> Childs {get; set;}
}
public class Child
{
public string Name{get; set;}
public string Value{get; set;}
public enum ValueType {get; set;}
}
public enum ValueType
{
Int,
Boolean,
String
};
public class ParentFactory
{
public List<Parent> Parents {get; set;}
public ParentFactory()
{
Child child1 = new Child() {Name="Filter1", Value="1", ValueType=ValueType.String};
Child child2 = new Child() {Name="isExecuted", Value="true", ValueType=ValueType.Boolean};
Child child3 = new Child() {Name="Width", Value="10", ValueType=ValueType.Int};
Parent parent1 = new Parent(){Name="Adam", Childs = new List<Child>(){child1, child2}};
Parent parent2 = new Parent(){Name="Kevin", Childs = new List<Child>(){child3}};
Parents = new List<Parent>(){parent1, parent2};
}
}
我要的对象绑定:ParentFactory parentFactory =新ParentFactory();到ItemsControl的:
I want to bind the object: ParentFactory parentFactory = new ParentFactory(); to ItemsControl:
<DockPanel>
<ItemsControl ItemsSource="{Binding Parents}">
</ItemsControl>
</DockPanel>
<Window.Resources>
<DataTemplate DataType="{x:Type Parent}">
<StackPanel Margin="2,2,2,1">
<Expander Header="{Binding Name}">
<ItemsControl ItemsSource="{Binding Childs}" />
</Expander>
</StackPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type Child}">
<StackPanel>
<TextBox Grid.Column="0" Text="{Binding Name}" />
<TextBox Grid.Column="1" Text="{Binding Value}"/>
<TextBox Grid.Column="2" Text="{Binding ValueType}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
在StackPanel中,有一个控制:文本框。不过,我希望它是更动态的:如果该值类型是一个布尔然后使用复选框,也可以使用一个文本框为:。
In the Stackpanel, there are one control: TextBox. However, I want it to be more dynamic: if the ValueType is a Boolean then use a Checkbox and else use a Textbox for the: .
有没有可能?如果是,我怎么能实现这些目标。请您提供一些代码示例?
Would it be possible? and if yes, how can I achieve them. could you please provide some code samples?
推荐答案
我认为做的最简单的方法是让他们都在你的面板并定义将控制他们的能见度
通过布尔能见度 ValueConverter
两个新的布尔属性。
I think the easiest way to do it is to have both of them in your panel and define two new boolean properties that will control their Visibility
through a Boolean To Visibility ValueConverter
.
public bool IsValueTypeBoolean
{
get
{
...Conditions that will return true for CheckBox.
}
}
public bool IsValueTypeOther
{
get
{
return !this.IsValueBoolean;
}
}
<TextBox Grid.Column="2" Visibility="{Binding IsValueTypeOther, Converter={StaticResource visibilityConverter}}"/>
<CheckBox Grid.Column="2" Visibility="{Binding IsValueTypeBoolean, Converter={StaticResource visibilityConverter}}"/>
Boolean要VisibilityConverter
Boolean To VisibilityConverter
[ValueConversion(typeof(bool), typeof(Visibility))]
public class BooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool myValue = (bool)value;
if (myValue)
return Visibility.Visible;
else
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
添加为资源,以你的XAML:
Add this as a resource to your XAML:
<local:BooleanToVisibilityConverter x:Key="visibilityConverter"></local:VisibilityConverter>
我希望不出现错别字...
I hope there are no typos...
这篇关于无论是使用复选框或文本框为枚举类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!