填充添加到Window 8.1-10但不是Windows 7的元素

时间:2017-11-06 13:45:43

标签: c# wpf windows-10 windows-8.1 windows-7-x64

我目前正致力于通过ListView显示项目层次结构的Visual Studio扩展。 WPF ListView包含样式的项目,就好像它们是树的一部分一样。它们的样式也可以将填充,边框厚度和边距减少到0.

可折叠或展开这些项目以隐藏其指定的子项。崩溃和扩展行为基本上包括添加/删除ListView中的项目以及更改Expander元素的状态。如果项目数量太大而无法包含在窗口中,则会根据需要显示滚动条。

通缉选择行为

enter image description here

我确实在大部分时间都得到了想要的行为。但是,有时,所有项目都会将其视图更改为:

随机发生的随机行为

enter image description here

当滚动条出现或消失时,问题会一直重复,但滚动条的状态也不会发生任何变化。我试过VS2015Enterprise和VS2017 Enterprise / Professional,问题只发生在Windows 8.1 / 10上。它在Windows 7上运行良好。

您是否有任何关于可能导致此添加的填充/边距/边框的想法?

(我已经看过不同基本风格的解决方案并为使用过的元素添加默认样式没有帮助。)

编辑:这是用于项目/树视图的样式:

<!-- Tree style -->

<Style x:Key="{x:Type custom:IssuesTreeViewInternal}" TargetType="{x:Type custom:IssuesTreeViewInternal}">
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type custom:IssuesTreeViewInternal}">
                <ScrollViewer x:Name="_tv_scrollviewer_" Focusable="False" CanContentScroll="True">
                    <ItemsPresenter />
                </ScrollViewer>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<!-- ExpandCollapseToggleStyle style -->

<Style x:Key="ExpandCollapseToggleStyle" TargetType="ToggleButton">
    <Setter Property="Focusable" Value="False" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToggleButton">
                <Grid Width="15" Height="13" Background="Transparent">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CheckStates">
                            <VisualState x:Name="Checked">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="Collapsed">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Hidden}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="Expanded">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Unchecked" />
                            <VisualState x:Name="Indeterminate" />
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Path x:Name="Collapsed" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="1,1,1,2" Data="M 4.5 0.5 L 8.5 4.5 L 4.5 8.5 Z" Stroke="{DynamicResource {x:Static vsUI:EnvironmentColors.StartPageTextDateBrushKey}}" />
                    <Path x:Name="Expanded" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="1,0,1,2" Data="M 9.5 0.5 L 9.5 5.5 L 4.5 5.5 Z" Visibility="Hidden" Stroke="{DynamicResource {x:Static vsUI:EnvironmentColors.StartPageTextDateBrushKey}}" Fill="{DynamicResource {x:Static vsUI:EnvironmentColors.StartPageTextDateBrushKey}}" />
                </Grid>
                <ControlTemplate.Triggers>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="true"/>
                        </MultiDataTrigger.Conditions>
                        <Setter Property="Stroke" TargetName="Collapsed" Value="{DynamicResource {x:Static vsUI:EnvironmentColors.ScrollBarArrowGlyphPressedBrushKey}}" />
                        <Setter Property="Stroke" TargetName="Expanded" Value="{DynamicResource {x:Static vsUI:EnvironmentColors.ScrollBarArrowGlyphPressedBrushKey}}" />
                        <Setter Property="Fill" TargetName="Expanded" Value="{DynamicResource {x:Static vsUI:EnvironmentColors.ScrollBarArrowGlyphPressedBrushKey}}" />
                    </MultiDataTrigger>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="true"/>
                        </MultiDataTrigger.Conditions>
                        <Setter Property="Fill" TargetName="Collapsed" Value="{DynamicResource {x:Static vsUI:EnvironmentColors.StartPageTextDateBrushKey}}" />
                        <Setter Property="Fill" TargetName="Expanded" Value="Transparent" />
                    </MultiDataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<!-- TreeViewItemFocusVisual style -->

<Style x:Key="TreeViewItemFocusVisual">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate>
                <Rectangle Margin="0" Stroke="{DynamicResource {x:Static vsUI:EnvironmentColors.SystemHighlightBrushKey}}" StrokeThickness="1" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<!-- IssuesTreeViewItem style -->
<Style x:Key="{x:Type custom:IssuesTreeViewItem}" TargetType="{x:Type custom:IssuesTreeViewItem}">
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="Padding" Value="1" />
    <Setter Property="Foreground" Value="{DynamicResource {x:Static vsUI:EnvironmentColors.ToolboxContentTextBrushKey}}" />
    <Setter Property="Focusable" Value="False" />   <!-- No need to focus on it. We can have focus on the parent ListViewItem instead. -->
    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    <Setter Property="BorderThickness" Value="0" /> <!-- 0 border thickness makes sure clicking on the IssuesTreeViewItem is registered everywhere, even in the border area. -->
    <Setter Property="ToolTip" Value="{Binding Path=Content.Tooltip}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type custom:IssuesTreeViewItem}">
                <ControlTemplate.Resources>
                    <converters:LeftMarginMultiplierConverter x:Key="LeftMarginMultiplier" DefaultLength="19" />
                </ControlTemplate.Resources>
                <Border Name="Border" 
                        Background="{TemplateBinding Background}" 
                        BorderBrush="{TemplateBinding BorderBrush}" 
                        BorderThickness="{TemplateBinding BorderThickness}" 
                        Padding="{TemplateBinding Padding}">
                    <Grid Margin="{Binding Converter={StaticResource LeftMarginMultiplier}, RelativeSource={RelativeSource TemplatedParent}}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="19" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <ToggleButton x:Name="Expander" 
                                      Style="{StaticResource ExpandCollapseToggleStyle}" 
                                      IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}" 
                                      ClickMode="Press" 
                                      VerticalAlignment="Top" 
                                      Margin="0, 2, 0, 0" 
                                      Command="{Binding Path=ToggleButtonCommand, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}" />
                        <ContentPresenter x:Name="PART_Header" 
                                          Grid.Column="1" 
                                          Content="{Binding Content}"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="HasChildren" Value="False">
                        <Setter TargetName="Expander" 
                                Property="Visibility" 
                                Value="Hidden" />
                    </Trigger>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding IsSelected}" Value="True" />
                            <Condition Binding="{Binding RelativeSource={RelativeSource AncestorType=Control}, Path=IsKeyboardFocusWithin}" Value="False" />
                        </MultiDataTrigger.Conditions>
                        <Setter TargetName="Border" 
                                Property="Background" 
                                Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=PassiveHighlightBrush}" />
                        <Setter TargetName="Border" 
                                Property="BorderBrush" 
                                Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=PassiveHighlightBrush}" />
                    </MultiDataTrigger>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding IsSelected}" 
                                       Value="False" />
                            <Condition Binding="{Binding Path=Content.IsSuppressed, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
                                       Value="True" />
                        </MultiDataTrigger.Conditions>
                        <Setter Property="Foreground" 
                                Value="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource TextViewItemForegroundColorConverter}}" />
                    </MultiDataTrigger>

                    <DataTrigger Binding="{Binding IsSelected}" Value="True">
                        <Setter TargetName="Border" Property="Background" Value="{DynamicResource {x:Static vsUI:EnvironmentColors.SystemHighlightBrushKey}}" />
                        <Setter TargetName="Border" Property="BorderBrush" Value="{DynamicResource {x:Static vsUI:EnvironmentColors.SystemHighlightBrushKey}}" />
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static vsUI:EnvironmentColors.SystemHighlightTextBrushKey}}" />
                    </DataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我们在VS Extension的XAML位中使用TreeView Style:

<local:IssuesTreeViewInternal 
            x:Name="issuesTreeInternal" 
            ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
            HorizontalContentAlignment="Stretch"
            VirtualizingPanel.IsVirtualizing="True" 
            VirtualizingPanel.VirtualizationMode="Recycling" 
            SelectionMode="Extended"
            DataContext="{Binding IssuesTreeViewInternalViewModel}"
            ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=IssuesTreeItems}"
            SelectedItem="{Binding SelectedItem}"
            behaviours:MultiSelectorBehaviours.SynchronizedSelectedItems="{Binding SelectedNames}" 
            x:FieldModifier="internal">
            <local:IssuesTreeViewInternal.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="FocusVisualStyle" Value="{StaticResource TreeViewItemFocusVisual}" />
                </Style>
            </local:IssuesTreeViewInternal.ItemContainerStyle>
        </local:IssuesTreeViewInternal>

IssueTreeViewInternal是一个继承自ListView的类。

1 个答案:

答案 0 :(得分:0)

我遇到了完全相同的问题。在Windows 10中添加了一些间距

这个SO答案解决了它: https://stackoverflow.com/a/15618991/4117068

这就是我所做的

<ListView ...>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="Margin" Value="0"/>
            <Setter Property="Padding" Value="0"/>
            <Setter Property="BorderThickness" Value="0"/>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>