Do I need to create a ControlTemplate? Or is there an alternative?

I got TreeView

and want to display data nested (not hierarchically). The first level TaskViewModel

data is called , the second level data ArtifactViewModel

. I want a horizontal one ArtifactViewModel

inside GroupBox

that represents TaskViewModel

. I've tried different approaches, this is my last one:

<TreeView Name="tvTasks" ItemsSource="{Binding Tasks}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type vm:TaskViewModel}">
            <GroupBox Header="{Binding Name, UpdateSourceTrigger=PropertyChanged}">
                <StackPanel Orientation="Vertical">
                    <ListView ItemsSource="{Binding Children}"/>
                    <TextBlock Text="{Binding Description, UpdateSourceTrigger=PropertyChanged}" TextWrapping="Wrap"/>
                </StackPanel>
            </GroupBox>
        </HierarchicalDataTemplate>
        <DataTemplate DataType="{x:Type vm:ArtifactViewModel}">
            <Border Background="{Binding Type,Converter={StaticResource Type2Background}}"
                    Margin="5" BorderBrush="Black" BorderThickness="2" CornerRadius="2">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="80"/>
                        <RowDefinition Height="20"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="100"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}"
                               TextAlignment="Center" Background="Black" Foreground="White"
                               Opacity="0.75" Grid.Column="0" Grid.Row="1"/>
                </Grid>
            </Border>
        </DataTemplate>                                          
    </TreeView.Resources>
</TreeView>

      

This is very similar to what I want, except it is ArtifactViewModel

shown vertically. And if I click on ArtifactViewModel

, tvTasks.SelectedItem doesn't change because it ListView

passes this event. I know this approach is not the smartest, but this is just an attempt.
I have looked through this article but I cannot see how to handle the different objects that I want to put in TreeView

. So ... how can I create an interface like this?

+1


a source to share


1 answer


The main problem you are facing is that you are inserting multiple controls, each with their own controls.

If you plan on displaying data as nested, but not hierarchically, then worry not using a TreeView. If you want one item to be selectable at any given time, use a ListBox instead.



Now the tricky part is playing with how you want to lay out the items. Take a look at Bea Stollnitz's example here , where she redraws the ListBox as a canvas. You can do something like this if the ItemsPanelTemplate is a canvas and you are calculating the x, y coordinates. Alternatively you can use Grid and define the Grid.Row and Grid.Column values.

0


a source







All Articles