Reputation: 20644
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 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 Age}"/>
<CheckBox Grid.Column="2" IsChecked="{Binding Married}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
In the Stackpanel, there are two types of controls: TextBox and CheckBox. However, I want them to be more dynamic: if the value is a boolean then use a Checkbox and else use a Textbox. That means I dont need to define the control either TextBox or Checkbox inside the StackPanel due to a numerous attributes in my Child class. Would it be possible, and if yes, how can I achieve them?
Upvotes: 4
Views: 1490
Reputation: 21863
I have done a solution from what i understood from your question. Please have a look at it. The sample is based on the DataTrigger and you can change the logics 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>
Upvotes: 2
Reputation: 4885
You can change the DataTemplate
dynamically
<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 the DataTemplates
for Condition1
and Condition2
hope it helps :)
Upvotes: 3
Reputation: 451
Check out this article : http://www.drwpf.com/blog/Home/tabid/36/EntryID/24/Default.aspx I think it 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>
Upvotes: 1