Using either a check box or text box for an enumeration type

If I have this structure:

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}; } } 

I want to bind an 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 has one control: TextBox. However, I want it to be more dynamic: if ValueType is logical, then use the checkbox, and then use the text box for: <TextBox Grid.Column="1" Text="{Binding Value}"/> .

Is this possible, and if so, how can I achieve it?

0
source share
2 answers

I think the easiest way to do this is to have both of them in your panel and define two new logical properties that will control their Visibility through the 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 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(); } } 

Add this as a resource to your XAML:

  <local:BooleanToVisibilityConverter x:Key="visibilityConverter"></local:VisibilityConverter> 

I hope there are no typos ...

+1
source

You can dynamically change the DataTemplate

Xaml

  <DataTemplate> <DataTemplate.Resources> <DataTemplate x:Key="Condition1"> <TextBox></TextBox> // Do you binding </DataTemplate> <DataTemplate x:Key="Condition2"> <CheckBox></CheckBox> // Do you binding </DataTemplate> </DataTemplate.Resources> </DataTemplate> <ContentPresenter x:Name="ContentField" Content="{Binding}" ContentTemplate="{StaticResource ResourceKey=Condition1}" /> <DataTemplate.Triggers> <DataTrigger Binding="{Binding Path=ValueType}" Value="1"> <Setter TargetName="ContentField" Property="ContentTemplate" Value="{StaticResource ResourceKey=Condition2}" /> </DataTrigger> </DataTemplate.Triggers> 

Be sure to set the bindings correctly ... and make DataTemplates for Condition1 and Condition2

hope this helps :)

+2
source

Source: https://habr.com/ru/post/913498/


All Articles