样式ListView(用于自定义模板)WPF

时间:2011-02-23 20:33:21

标签: c# wpf xaml expression-blend

关于样式ListView我几乎没有问题。例如,我有一些风格:

    <Style x:Key="MyListView" TargetType="{x:Type ListView}">
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
    <Setter Property="BorderBrush" Value="{StaticResource ListBorder}"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden"/>
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListView}">
                <Border x:Name="PART_ControlBorder" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="0">
                   <Grid>
                        <ScrollViewer Grid.Row="1"
                            VerticalScrollBarVisibility="Hidden"
                            HorizontalScrollBarVisibility="Hidden"
                            CanKeyboardScroll="False"
                            Padding="{TemplateBinding Padding}" 
                            Focusable="false">
                            <ItemsPresenter x:Name="PART_ItemsPresenter" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </ScrollViewer>
                    </Grid>
                </Border> 
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <Border x:Name="ItemBorder" CornerRadius="4" SnapsToDevicePixels="true" BorderBrush="{TemplateBinding BorderBrush}"  BorderThickness="2" Padding="1">
                    <Grid>
                        <TextBlock Text="{Binding Key}" Background="LightGray"/>
                    </Grid>
                </Border>
            </DataTemplate>
        </Setter.Value>
    </Setter>

    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background"  Value="{x:Null}" />
                        <Setter Property="BorderBrush" Value="{x:Null}" />
                        <Setter Property="Foreground" Value="Black" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Setter.Value>
    </Setter>
</Style>
  1. 为什么Backgroud和BorderBrush的setter不起作用(ItemContainerStyle)?我不得不使用系统画笔的重新定义隐藏选择,但错误的方法是:

    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#00000000"/>
    

  2. 如何为 ItemBorder 设置BorderBrush(在mouseOver事件上)?

  3. 如何为 ItemBorder 设置BorderBrush(仅适用于所选项目)?

  4. 如何更改默认选择样式?

1 个答案:

答案 0 :(得分:1)

我自己找到了答案。 要为ItemBorder设置样式,我必须创建几个DataTriggers并将它们放入DataTemplate:

                    <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListViewItem}},Path=IsSelected}" Value="True">
                        <Setter TargetName="ItemBorder" Property="BorderBrush" Value="Lime"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListViewItem}}, Path=IsMouseOver}" Value="True">
                        <Setter TargetName="ItemBorder" Property="BorderBrush" Value="Orange"/>
                    </DataTrigger>
                </DataTemplate.Triggers>

关于问题1和4我仍然没有回答...(如果我不计算更改默认系统画笔的方法)