Question

I'm working right now with the TreeView. As you can see CurrentMediumObjectives and ChildrenObjective are the same object type called Objective.

This class has a property called Level, where in ChildrenObjective is set to 1 and ChildrenObjective is set to 2.

I would like to set a different DataTemplate depending on the Level.

    <TreeView ItemsSource="{Binding CurrentMediumObjectives}"
        Margin="0,40,0,0">     
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding ChildrenObjective}">
                <TextBlock FontWeight="Bold">
                    <Run Text="Objective " />
                    <Run Text="{Binding Level}" />
                </TextBlock>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

I'm confusing about if should I use a DataTemplateSelector or a DataTrigger. Thanks in advance.

Was it helpful?

Solution

Perhaps you could just use a Trigger to show each template.

Somthing like this, maybe:

<TreeView ItemsSource="{Binding CurrentMediumObjectives}" Margin="0,40,0,0">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding ChildrenObjective}">
                    <Grid>
                        <!--Data template for level 1-->
                        <TextBlock x:Name="level1" FontWeight="Bold">
                            <Run Text="Objective " />
                            <Run Text="{Binding Level}" />
                        </TextBlock>

                        <!--Data template for level 2-->
                        <TextBlock x:Name="level2" FontWeight="Bold" Foreground="Red">
                            <Run Text="Objective " />
                            <Run Text="{Binding Level}" />
                        </TextBlock>
                   </Grid>

                    <HierarchicalDataTemplate.Triggers>
                        <DataTrigger Binding="{Binding Level}" Value="1">
                            <Setter TargetName="level1" Property="Visibility" Value="Visible" />
                            <Setter TargetName="level2" Property="Visibility" Value="Collapsed" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Level}" Value="2">
                            <Setter TargetName="level2" Property="Visibility" Value="Visible" />
                            <Setter TargetName="level1" Property="Visibility" Value="Collapsed" />
                        </DataTrigger>
                    </HierarchicalDataTemplate.Triggers>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>

Model (Guess)

public class Objective 
{
    public int Level { get; set; }
}

public class CurrentMediumObjectives : Objective
{
    public ObservableCollection<ChildrenObjective> ChildrenObjective { get; set; }
}

public class ChildrenObjective : Objective
{
}

Result: enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top