问题
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>
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"/>
How could I set BorderBrush for ItemBorder (on mouseOver event)?
How could I set BorderBrush for ItemBorder(only for selected Items)?
What shall I do to change default selection style?
回答1:
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>
About question 1 and 4 I still haven't answer...(If I won't count way to change default system brushes)
来源:https://stackoverflow.com/questions/5096750/styling-listviewfor-custom-template-wpf