1

Is it possible to display something in my treeview at design time and what would be the best way to achieve it?

Runtime works perfectly well and does display the data I want. However, I would like to have a more convenient display when designing than an empty area where the treeview is.

Thank you.

The treeview looks like this:

       <TreeView x:Name="tvConfig" Width="400" Height="300"  >

        <TreeView.ItemTemplate>

            <HierarchicalDataTemplate DataType="{x:Type demo:TvItemsSource}" ItemsSource="{Binding Path=Items}">
                <StackPanel Orientation="Horizontal">
                    <Image></Image>
                    <TextBlock Text="{Binding Name}"></TextBlock>
                </StackPanel>

            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

And make uses of this class:

Public Class TvItemsSource
 Public Property Items() As New List(Of TvItemsSource)
 Public Property Name() As String

 Public Sub New(Name As String)
    Me.Name = Name
 End Sub

End Class

I generated a small runtime data using the following piece of code:

Private _source As TvItemsSource
Private Sub LoadData()
  _source = New TvItemsSource("Root")
  _source.Items.Add(New TvItemsSource("Item 1"))
  _source.Items.Add(New TvItemsSource("Item 2 "))
    Dim ParentItem1 = New TvItemsSource("Parent 1")
    ParentItem1.Items.Add(New TvItemsSource("Enfant 1"))
    Dim ParentItem2 = New TvItemsSource("Parent 2")
    ParentItem2.Items.Add(New TvItemsSource("Enfant 2"))
    ParentItem1.Items.Add(ParentItem2)
    _source.Items.Add(ParentItem1)

    tvConfig.ItemsSource = _source.Items
End sub

edit: In the xaml of the treeview, I added the xmlns namespace of the application to declare the data type as :

xmlns:demo="clr-namespace:demo"
Trevor
  • 7,777
  • 6
  • 31
  • 50
Sage Pourpre
  • 9,932
  • 3
  • 27
  • 39

1 Answers1

0

I just found the ideal solution for my project by using this statement:

Dim IsDesignMode As Boolean = DesignerProperties.GetIsInDesignMode(New DependencyObject())

The IsDesignMode variable does return true and anything therefore a listbox items source binded on a "Content" property would display the "Design Mode" items in the designer and the "Production" items when ran.

If IsDesignMode Then
        For I = 1 To 10
            Content.Add("DesignMode " & I)
        Next
    Else
        Content.Add("Production 1")
        Content.Add("Production 2")
        Content.Add("Production 3")
    End If

There are other way to do it.

Relevant: SO: What approaches are available to dummy design-time data in WPF

Also: Simulating data in design mode in Microsoft Blend

Community
  • 1
  • 1
Sage Pourpre
  • 9,932
  • 3
  • 27
  • 39