简体   繁体   中英

Treeview shows random extra space

I have a treeview in wpf that is showing some strange behavior (at least I think it's strange). I don't see anywhere I can attach an image but basically on some of the items it looks like it's double spaced or has an extra 3px or so margin and on some it looks like it's single spaced. Here is the XAML defining it:

        <TreeView Margin="5,0,0,5" ItemsSource="{Binding SortedCategories}" HorizontalContentAlignment="Stretch">
            <TreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type COBie:CategoryData}" ItemsSource="{Binding SortedTypes}">
                <CheckBox IsChecked="{Binding AnyChecked, Mode=TwoWay}" Content="{Binding CategoryName}" IsEnabled="{Binding HasTypes}" />
            </HierarchicalDataTemplate>
                <DataTemplate DataType="{x:Type COBie:InstanceData}">
                    <CheckBox IsChecked="{Binding isChecked, Mode=TwoWay}" Content="{Binding ElemID}" />
                </DataTemplate>
                <DataTemplate DataType="{x:Type COBie:TypeData}">
                    <TreeViewItem IsEnabled="{Binding HasInstances}" HorizontalContentAlignment="Stretch">
                        <TreeViewItem.Header>
                            <CheckBox IsChecked="{Binding isChecked, Mode=TwoWay}" Content="{Binding ElemName}" />
                        </TreeViewItem.Header>
                        <ListView ItemsSource="{Binding SortedInstances}">
                            <ListView.View>
                                <GridView>
                                    <GridViewColumn>
                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <CheckBox IsChecked="{Binding isChecked, Mode=TwoWay}" />
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>
                                    <GridViewColumn Header="ID" DisplayMemberBinding="{Binding ElemID}" />
                                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding ElemName}" />
                                    <GridViewColumn Header="Mark" DisplayMemberBinding="{Binding Mark}" />
                                    <GridViewColumn Header="Room" DisplayMemberBinding="{Binding RoomDisplay}" />
                                </GridView>
                            </ListView.View>
                        </ListView>
                    </TreeViewItem>
                </DataTemplate>
            </TreeView.Resources>
        </TreeView>

The only other relevant style that would apply is:

        <Style TargetType="TreeViewItem">
            <Setter Property="IsExpanded" Value="{Binding IsExpanded}" />
        </Style>

I don't see anywhere that defines a margin at all and even if it did all of the elements are of the same class so it should apply to all equally.

Anyone know what is going on?

I had the same issue, that there were seemingly random gaps. After looking closer I found out that the gaps appeared around items, that have underscore(s) in the name.

If you put the string directly in the content property of the CheckBox and it contains underscores, the CheckBox will surround a TextBlock with a Control named AccessText, probably to support short cut keys (similar to underscores in Button's Content property).

Now for some reason the Margin of the Checkbox is set on the TextBlock perfectly fine, if there is no AccessText involved, but is not set on the TextBlock inside the AccessText. This way the TextBlock inside the AccessText will have default TextBlock Style, which in my case was with Margin=5.

I guess in your case it must be a closely related issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM