WPF圆角边框ListView剪辑问题

时间:2013-05-26 08:00:37

标签: wpf listview border rounded-corners clip

我有一个带圆角边框的ListView

当您点击ListVIewItem时,会出现一个伸出ListView的箭头(通过更改边距)。

一开始看起来很不错,但是一旦你点击一个项目,第一个和最后一个项目就会突出ListView的圆形边框。

为什么会发生这种情况,如何解决?

Unselected

Selected

相关守则:

<Window x:Class="WPFJonnyStyle.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing" 
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <ResourceDictionary>
        <ControlTemplate x:Key="ArrowedItemsControl" TargetType="{x:Type ItemsControl}">                
            <Border BorderBrush="#FF9DD3ED" BorderThickness="2" CornerRadius="15"  >
                <Grid>
                <Border Name="mask" Background="White" BorderThickness="2" CornerRadius="15"  />
                    <ItemsPresenter>
                        <ItemsPresenter.OpacityMask>
                            <VisualBrush Visual="{Binding ElementName=mask}" />
                        </ItemsPresenter.OpacityMask>
                    </ItemsPresenter>
                </Grid>
            </Border>  
        </ControlTemplate>

        <ControlTemplate x:Key="ArrowedItem" TargetType="ListViewItem">
            <Border BorderThickness="0 0 0 1" BorderBrush="#FF9DD3ED" CornerRadius="15">

                <Grid Background="White" Height="40">

                    <ed:BlockArrow Margin="-22 0 0 0"  x:Name="fancyArrow" Fill="#FF0C8CCB" Visibility="Collapsed"
                                       FlowDirection="RightToLeft" ArrowBodySize="1" 
                                       Height="40" StrokeThickness="2" 
                                       VerticalAlignment="Center"/>

                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                              Margin="{TemplateBinding Padding}" 
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                              RecognizesAccessKey="True"/>

                </Grid>
            </Border>

            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="Background" Value="White"/>
                    <Setter Property="Foreground" Value="Black"/>
                    <Setter Property="FontWeight" Value="Bold"/>
                </Trigger>
                <Trigger Property="IsFocused" Value="True">
                    <Setter Property="Background" Value="Transparent"/>
                    <Setter Property="Foreground" Value="White"/>
                    <Setter Property="FontWeight" Value="ExtraBold"/>
                    <Setter Property="ed:BlockArrow.Visibility" TargetName="fancyArrow" Value="Visible"/>                        
                </Trigger>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsFocused" Value="False"/>
                        <Condition Property="IsMouseOver" Value="False" />
                    </MultiTrigger.Conditions>
                    <Setter Property="Background" Value="White" />
                    <Setter Property="Foreground" Value="Black"/>
                    <Setter Property="FontWeight" Value="Regular"/>                        
                </MultiTrigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

        <Style TargetType="ListViewItem">
            <Setter Property="Template" Value="{DynamicResource ArrowedItem}"/>
        </Style>

    </ResourceDictionary>
</Window.Resources>

<Grid Background="#FFEFEFF2">
    <ListView Width="100" Height="250" Template="{DynamicResource ArrowedItemsControl}" HorizontalContentAlignment="Center">
        <ListViewItem >Car</ListViewItem>
        <ListViewItem >Tractor</ListViewItem>
        <ListViewItem >Train</ListViewItem>
        <ListViewItem >Plane</ListViewItem>
        <ListViewItem >Rocket</ListViewItem>
        <ListViewItem >Helicopter</ListViewItem>
    </ListView>
</Grid>

1 个答案:

答案 0 :(得分:3)

Download Link

主要问题是ListViewItem的白色Background在渲染时已应用于ListView的{​​{1}} Border。因此,设置为Transparent解决了主要问题,但由于此更改(例如Border之间的分隔符ListViewItem裁剪

,因此还需要一些其他修复程序

附加下载项目中与此问题相关的所有内容都在“MainWindow.xaml”