简体   繁体   English

Treeview显示随机的额外空间

[英]Treeview shows random extra space

I have a treeview in wpf that is showing some strange behavior (at least I think it's strange). 我在wpf中有一个treeview显示了一些奇怪的行为(至少我认为这很奇怪)。 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. 我看不到任何可以附加图像的地方,但基本上在某些项目上看起来像是双倍行距或有额外的3px左右的边距,而在某些项目上看起来像是单行距。 Here is the XAML defining it: 这是定义它的XAML:

        <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). 如果将字符串直接放在CheckBox的content属性中,并且包含下划线,则CheckBox将用一个名为AccessText的控件包围TextBlock,这可能支持快捷键(类似于Button的Content属性中的下划线)。

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. 现在,由于某种原因,如果不涉及AccessText,则在TextBlock上设置Checkbox的边距就可以了,但是在AccessText内的TextBlock上没有设置Checkbox的边距。 This way the TextBlock inside the AccessText will have default TextBlock Style, which in my case was with Margin=5. 这样,AccessText中的TextBlock将具有默认的TextBlock样式,在我的情况下是Margin = 5。

I guess in your case it must be a closely related issue. 我想您的情况一定是一个密切相关的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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