在通用应用程序中为 ListViewItem 定义自定义样式
define a custom style to a ListViewItem in a universal app
我正在尝试将此样式设置为我的 ListViewItems,当鼠标悬停在它们上时:
我已经修改了 App.xaml 文件中的样式,但这并没有解决我的问题
<SolidColorBrush x:Key="ListViewItemPointerOverBackgroundThemeBrush" Color="#d9d7ec" />
然后,当鼠标悬停在上面时,我做了一个 Style,但这都不行
<Style x:Key="listItemmenuStyle"
TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="PointerOver">
<Storyboard>
<ColorAnimation Duration="0" To="#d9d7ec" Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="listItem1" />
<ColorAnimation Duration="0" To="#393185" Storyboard.TargetProperty="(ListViewItem.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="Content1" />
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<ColorAnimation Duration="0" To="#d9d7ec" Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="listItem1" />
<ColorAnimation Duration="0" To="#393185" Storyboard.TargetProperty="(ListViewItem.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="Content1" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid>
<Rectangle x:Name="listItem1" Stroke="Transparent" Fill="Transparent" Margin="0"/>
<ContentPresenter x:Name="Content1"/>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这就是我将此样式应用到 ListView 的方式:
<ListView Style="{Binding Color, Source={StaticResource listItemmenuStyle}}">
<ListView.ItemTemplate >
<DataTemplate>
<StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
<TextBlock x:Name="day" Text="{Binding Path=day}" Foreground="#797978"></TextBlock>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate >
</ListView
请问如何修改样式以具有如上图所示的 ListviewItem
感谢帮助
更新:
我尝试了这个解决方案,当我悬停时它改变了 ListViewItem 的背景颜色,但是当我悬停在每个 ListViewItem 上时,我总是在定义前景色时遇到问题,即使 PointerOverForeground 颜色属性发生了变化:
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<ListViewItemPresenter
FocusSecondaryBorderBrush="#d9d7ec"
PlaceholderBackground="#d9d7ec"
PointerOverBackground="#d9d7ec"
PointerOverForeground="#342d65"
SelectedBackground="#d9d7ec"
SelectedForeground="#d9d7ec"
SelectedPointerOverBackground="#342d65"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Center"
Foreground="#342d65"
/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
不要将 Foreground
颜色设置为与 PointerOverForeground
颜色相同,将鼠标悬停在项目上时无法区分颜色。我认为你应该为 Foreground
和 PointerOverForeground
设置不同的颜色。根据您的图片,也许 Foreground
应该设置为 Gray
:
<ListView Name="myList">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<ListViewItemPresenter
FocusSecondaryBorderBrush="#d9d7ec"
PlaceholderBackground="#d9d7ec"
PointerOverBackground="#d9d7ec"
PointerOverForeground="#342d65"
SelectedBackground="#d9d7ec"
SelectedForeground="#342d65"
SelectedPointerOverBackground="#d9d7ec"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Center"
Foreground="Gray" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
</ListView>
我正在尝试将此样式设置为我的 ListViewItems,当鼠标悬停在它们上时:
我已经修改了 App.xaml 文件中的样式,但这并没有解决我的问题
<SolidColorBrush x:Key="ListViewItemPointerOverBackgroundThemeBrush" Color="#d9d7ec" />
然后,当鼠标悬停在上面时,我做了一个 Style,但这都不行
<Style x:Key="listItemmenuStyle"
TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="PointerOver">
<Storyboard>
<ColorAnimation Duration="0" To="#d9d7ec" Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="listItem1" />
<ColorAnimation Duration="0" To="#393185" Storyboard.TargetProperty="(ListViewItem.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="Content1" />
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<ColorAnimation Duration="0" To="#d9d7ec" Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="listItem1" />
<ColorAnimation Duration="0" To="#393185" Storyboard.TargetProperty="(ListViewItem.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="Content1" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid>
<Rectangle x:Name="listItem1" Stroke="Transparent" Fill="Transparent" Margin="0"/>
<ContentPresenter x:Name="Content1"/>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这就是我将此样式应用到 ListView 的方式:
<ListView Style="{Binding Color, Source={StaticResource listItemmenuStyle}}">
<ListView.ItemTemplate >
<DataTemplate>
<StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
<TextBlock x:Name="day" Text="{Binding Path=day}" Foreground="#797978"></TextBlock>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate >
</ListView
请问如何修改样式以具有如上图所示的 ListviewItem 感谢帮助
更新: 我尝试了这个解决方案,当我悬停时它改变了 ListViewItem 的背景颜色,但是当我悬停在每个 ListViewItem 上时,我总是在定义前景色时遇到问题,即使 PointerOverForeground 颜色属性发生了变化:
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<ListViewItemPresenter
FocusSecondaryBorderBrush="#d9d7ec"
PlaceholderBackground="#d9d7ec"
PointerOverBackground="#d9d7ec"
PointerOverForeground="#342d65"
SelectedBackground="#d9d7ec"
SelectedForeground="#d9d7ec"
SelectedPointerOverBackground="#342d65"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Center"
Foreground="#342d65"
/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
不要将 Foreground
颜色设置为与 PointerOverForeground
颜色相同,将鼠标悬停在项目上时无法区分颜色。我认为你应该为 Foreground
和 PointerOverForeground
设置不同的颜色。根据您的图片,也许 Foreground
应该设置为 Gray
:
<ListView Name="myList">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<ListViewItemPresenter
FocusSecondaryBorderBrush="#d9d7ec"
PlaceholderBackground="#d9d7ec"
PointerOverBackground="#d9d7ec"
PointerOverForeground="#342d65"
SelectedBackground="#d9d7ec"
SelectedForeground="#342d65"
SelectedPointerOverBackground="#d9d7ec"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Center"
Foreground="Gray" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
</ListView>