'Getting exception Width and Height must be non-negative; while adding items to a Virtualized Treeview in WPF

I am using a virtualized Treeview using Ui virtualisation by setting properties in TreeStyle and using in the ItemsPanelTemplate. The treeview is getting virualized and able to scroll , but getting exception :"System.ArgumentException: Width and Height must be non-negative." while adding child items to treeview.

The treeviewStyle and TreeViewItem stlye which I have used are placed below:

 <Style x:Key="TreeViewStyle" TargetType="TreeView">
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="ScrollViewer.CanContentScroll" Value="True" />
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Visible" />
        <Setter Property="Stylus.IsFlicksEnabled" Value="False" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True" />
        <Setter Property="VirtualizingStackPanel.VirtualizationMode" Value="Standard" />
        <!--  TreeView ItemsPresenter StackPanel VirtualizingStackPanel  -->
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel Height="366" />
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TreeView">
                    <Border Name="Border"
                            Background="#232323"
                            BorderThickness="1"
                            CornerRadius="1">
                        <ScrollViewer Name="_tv_scrollviewer_"
                                      CanContentScroll="True"
                                      Focusable="False"
                                      Padding="5,1,0,2"
                                      Template="{StaticResource ScrollViewerControlTemplate3}">
                            <ItemsPresenter />
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style TargetType="{x:Type TreeViewItem}">
        <Style.Triggers>
            <Trigger Property="HasItems" Value="False">
                <Setter Property="Background" Value="Red" />
                <Setter Property="BorderBrush" Value="Blue" />
            </Trigger>
        </Style.Triggers>
    </Style>

    <!-- Placing items in the tree view  -->
    <Style x:Key="TreeViewItemStyle" TargetType="{x:Type TreeViewItem}">
        <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type 

ItemsControl}}}" />
        <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type 

ItemsControl}}}" />
        <Setter Property="Padding" Value="1,0,0,0" />
        <Setter Property="Margin" Value="-24,0,0,0" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="FocusVisualStyle" Value="{StaticResource TreeViewItemFocusVisual}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TreeViewItem}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" MinWidth="19" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <Border x:Name="Bd"
                                Grid.Column="1"
                                Margin="0,0,0,0"
                                BorderBrush="#505050"
                                BorderThickness="0,0,0,0"
                                Padding="{TemplateBinding Padding}"
                                SnapsToDevicePixels="true">
                            <!--  Header/Root Elements  -->
                            <ContentPresenter x:Name="PART_Header"
                                              Margin="2,0,0,0"
                                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                              ContentSource="Header"
                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </Border>
                        <!--  Child Elements  -->
                        <ItemsPresenter x:Name="ItemsHost"
                                        Grid.Row="1"
                                        Grid.Column="1"
                                        Grid.ColumnSpan="2"
                                        Margin="0,0,0,0" />
                    </Grid>

                    <ControlTemplate.Triggers>
                        <!--<Trigger Property="IsExpanded" Value="false">
                            <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
                        </Trigger>-->
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

I am using .Net version 4.5 VS2010.



Solution 1:[1]

For anyone interested: I had the problem within a DataTemplate of a ListBox set in the ItemTemplate.

  • Placing a simple TextBlock in the DataTemplate with the Text property bound to a ViewModel caused the error.
  • Removing the Binding and placing a text in the property removed the issue. The bound property of the ViewModel always contained values, so it had nothing to do with that.
  • Setting MinHeight and/or MinWidth did not solve the problem.
  • Setting Height and/or Width solved the problem.

In my case I wanted a dynamic height and width, so that did not help. In the end I set HorizontalAlignment=Stretch and VerticalAlignment=Stretch, which was the solution.

Quite an annoying issue.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chris