Using a text box or check box based on attribute 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 int Age {get; set;} public bool Married {get; set;} } public class ParentFactory { public List<Parent> 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<Child>(){child1, child2}}; Parent parent2 = new Parent(){Name="Kevin", Childs = new List<Child>(){child3}}; Parents = new List<Parent>(){parent1, parent2}; } } 

I want to bind the ParentFactory parentFactory = new ParentFactory() object ParentFactory parentFactory = new ParentFactory() to the 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 Age}"/> <CheckBox Grid.Column="2" IsChecked="{Binding Married}"/> </StackPanel> </DataTemplate> </Window.Resources> 

There are two types of controls in Stackpanel: TextBox and CheckBox. However, I want them to be more dynamic: if the value is logical, then use the checkbox and still use the text box. This means that I do not need to define a TextBox or Checkbox control inside the StackPanel due to the many attributes in my Child class. It would be possible, and if so, how can I achieve them?

+4
source share
3 answers

I made a decision from what I understood from your question. Please take a look at this. The sample is based on a DataTrigger, and you can change the logic to Converter.

 <Window x:Class="StackAnswers.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" xmlns:t="clr-namespace:StackAnswers"> <Window.Resources> <DataTemplate DataType="{x:Type t:Parent}"> <StackPanel Margin="2,2,2,1"> <Expander Header="{Binding Name}"> <ItemsControl ItemsSource="{Binding Childs}" /> </Expander> </StackPanel> </DataTemplate> <DataTemplate DataType="{x:Type t:Child}"> <StackPanel> <TextBlock Text="{Binding Name}"></TextBlock> <TextBox Grid.Column="0" Text="{Binding Name}"> <TextBox.Style> <Style TargetType="{x:Type TextBox}"> <Setter Property="Visibility" Value="Collapsed" /> <Style.Triggers> <DataTrigger Binding="{Binding Married}" Value="false"> <Setter Property="Visibility" Value="Visible" /> </DataTrigger> </Style.Triggers> </Style> </TextBox.Style> </TextBox> <TextBox Grid.Column="1" Text="{Binding Age}"> <TextBox.Style> <Style TargetType="{x:Type TextBox}"> <Setter Property="Visibility" Value="Collapsed" /> <Style.Triggers> <DataTrigger Binding="{Binding Married}" Value="false"> <Setter Property="Visibility" Value="Visible" /> </DataTrigger> </Style.Triggers> </Style> </TextBox.Style> </TextBox> <CheckBox Grid.Column="2" IsChecked="{Binding Married}" Content="Married"> <CheckBox.Style> <Style TargetType="{x:Type CheckBox}"> <Setter Property="Visibility" Value="Collapsed" /> <Style.Triggers> <DataTrigger Binding="{Binding Married}" Value="True"> <Setter Property="Visibility" Value="Visible"></Setter> </DataTrigger> </Style.Triggers> </Style> </CheckBox.Style> </CheckBox> </StackPanel> </DataTemplate> </Window.Resources> <DockPanel> <ItemsControl ItemsSource="{Binding Parents}"> </ItemsControl> </DockPanel> 

+2
source

You can dynamically change the DataTemplate

Xaml

  <DataTemplate> <DataTemplate.Resources> <DataTemplate x:Key="Condition1"></DataTemplate> <DataTemplate x:Key="Condition2"></DataTemplate> </DataTemplate.Resources> </DataTemplate> <ContentPresenter x:Name="ContentField" Content="{Binding}" ContentTemplate="{StaticResource ResourceKey=Condition1}" /> <DataTemplate.Triggers> <DataTrigger Binding="{Binding Path=IsMarried}" Value="True"> <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 :)

+3
source

Check out this article: http://www.drwpf.com/blog/Home/tabid/36/EntryID/24/Default.aspx I think this is what you need:

 <Page xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib"> <Page.Resources> <DataTemplate DataType="{x:Type sys:Boolean}"> <CheckBox IsChecked="{Binding Mode=OneWay}" /> </DataTemplate> </Page.Resources> <ItemsControl Width="100" Height="100"> <sys:Int32>30</sys:Int32> <sys:DateTime>12/16/1970</sys:DateTime> <sys:Boolean>True</sys:Boolean> <sys:Boolean>False</sys:Boolean> <sys:String>Foo</sys:String> </ItemsControl> </Page> 
+1
source

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


All Articles