3

我以这种方式应用了一个HierarchicalDataTemplate用于向树项添加图标的方法:

  <TreeView.ItemTemplate>
            <HierarchicalDataTemplate DataType="{x:Type DataAccess:IDataAccessible}" 
                                  ItemsSource="{Binding Path=Items}">                    
                    <StackPanel Orientation="Horizontal" Margin="2" DataContext="{Binding}">
                        <Image Width="16" Height="16" SnapsToDevicePixels="True"
                           Source="{Binding Converter={StaticResource treeImageConverter}}">
                    </Image>
                        <TextBlock Text="{Binding Path=Name}" Margin="5,0" />
                    </StackPanel>                    
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>

然后我添加ItemContainerStyle了隐藏箭头按钮:

 <TreeView.ItemContainerStyle>
            <Style TargetType="TreeViewItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="TreeViewItem" xmlns:s="clr-namespace:System;assembly=mscorlib">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" MinWidth="19" />
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition />
                                </Grid.RowDefinitions>
                                <!-- Make the ToggleButton invisible -->
                                <ToggleButton IsChecked="False" Visibility="Hidden" ClickMode="Press" Name="Expander" >

                                </ToggleButton>
                                <Border BorderThickness="{TemplateBinding Border.BorderThickness}" Padding="{TemplateBinding Control.Padding}" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" Name="Bd" SnapsToDevicePixels="True" Grid.Column="1">
                                    <ContentPresenter Content="{TemplateBinding HeaderedContentControl.Header}" ContentTemplate="{TemplateBinding HeaderedContentControl.HeaderTemplate}" ContentStringFormat="{TemplateBinding HeaderedItemsControl.HeaderStringFormat}" ContentSource="Header" Name="PART_Header" HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                                </Border>
                                <ItemsPresenter Name="ItemsHost" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="2" />
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="ItemsControl.HasItems">
                                    <Setter Property="UIElement.Visibility" TargetName="Expander">
                                        <Setter.Value>
                                            <x:Static Member="Visibility.Hidden" />
                                        </Setter.Value>
                                    </Setter>
                                    <Trigger.Value>
                                        <s:Boolean>False</s:Boolean>
                                    </Trigger.Value>
                                </Trigger>
                                <Trigger Property="TreeViewItem.IsSelected">
                                    <Setter Property="Panel.Background" TargetName="Bd">
                                        <Setter.Value>
                                            <DynamicResource ResourceKey="{x:Static SystemColors.HighlightBrushKey}" />
                                        </Setter.Value>
                                    </Setter>
                                    <Setter Property="TextElement.Foreground">
                                        <Setter.Value>
                                            <DynamicResource ResourceKey="{x:Static SystemColors.HighlightTextBrushKey}" />
                                        </Setter.Value>
                                    </Setter>
                                    <Trigger.Value>
                                        <s:Boolean>True</s:Boolean>
                                    </Trigger.Value>
                                </Trigger>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="TreeViewItem.IsSelected">
                                            <Condition.Value>
                                                <s:Boolean>True</s:Boolean>
                                            </Condition.Value>
                                        </Condition>
                                        <Condition Property="Selector.IsSelectionActive">
                                            <Condition.Value>
                                                <s:Boolean>False</s:Boolean>
                                            </Condition.Value>
                                        </Condition>
                                    </MultiTrigger.Conditions>
                                    <Setter Property="Panel.Background" TargetName="Bd">
                                        <Setter.Value>
                                            <DynamicResource ResourceKey="{x:Static SystemColors.ControlBrushKey}" />
                                        </Setter.Value>
                                    </Setter>
                                    <Setter Property="TextElement.Foreground">
                                        <Setter.Value>
                                            <DynamicResource ResourceKey="{x:Static SystemColors.ControlTextBrushKey}" />
                                        </Setter.Value>
                                    </Setter>
                                </MultiTrigger>
                                <Trigger Property="UIElement.IsEnabled">
                                    <Setter Property="TextElement.Foreground">
                                        <Setter.Value>
                                            <DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" />
                                        </Setter.Value>
                                    </Setter>
                                    <Trigger.Value>
                                        <s:Boolean>False</s:Boolean>
                                    </Trigger.Value>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </TreeView.ItemContainerStyle>

这就是它之前和之后的样子: 在此处输入图像描述

这种质量下降的原因是什么?

4

1 回答 1

2

LayoutRounding属性设置为 true。当布局计算后位置无法直接映射到屏幕像素,并且颜色值渗入两个显示像素时,就会发生这种情况。考虑放置在 y=3 上的水平线,它将用完整的颜色填充该线,如果将其放置在 y=3.5 上,则需要在 3 上填充一半,在 4 上填充一半。但没有一半像素显然,这就是为什么它混合了线条之间的颜色。所以你通常会在 y 3 和 4 上得到一条半透明的线。

有时这是需要的,尤其是动画,因为这种亚像素渲染使一切看起来更平滑,但对于图标,这通常是不可取的。

于 2013-09-04T13:29:42.410 回答