样式ListView(用于自定义模板)WPF [英] Styling ListView(for custom template) WPF

查看:229
本文介绍了样式ListView(用于自定义模板)WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对设置ListView的样式有几个问题.例如,我有一些风格:

I have few questions about styling ListView. For example I have some style:

    <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的设置器不起作用(ItemContainerStyle)?我必须使用系统笔刷的重新定义来隐藏选择,但这是错误的方式:

  1. Why are setters for Backgroud and BorderBrush not working(ItemContainerStyle)? I had to hide selection using redefinition for system Brushes, but its wrong way:

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

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

How could I set BorderBrush for ItemBorder (on mouseOver event)?

如何为 ItemBorder (仅适用于选定的项目)设置BorderBrush?

How could I set BorderBrush for ItemBorder(only for selected Items)?

如何更改默认选择样式?

What shall I do to change default selection style?

推荐答案

我自己找到了答案. 要设置ItemBorder的样式,我必须创建一些DataTriggers并将其放入DataTemplate:

I'd found answer by myself. To set style for ItemBorder I had to create few DataTriggers and put them into 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,我仍然没有回答...(如果我不算改变默认系统画笔的方式)

About question 1 and 4 I still haven't answer...(If I won't count way to change default system brushes)

这篇关于样式ListView(用于自定义模板)WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆