【发布时间】:2016-07-21 19:44:24
【问题描述】:
我对通用应用程序和 XAML 比较陌生,我正在尝试将样式应用于我的主页。到目前为止,我已经成功地为ListBoxItems 定义了一个样式,但是我找不到在选择项目时更改背景填充颜色的方法。
我知道在 WPF 中设置触发器并在触发事件时更改项目的属性非常简单,但是通用应用程序中没有触发器。
我的目标是在选中 ListBoxItem 时将其背景属性设置为 Gray,但是我找不到实现此目的的方法。我尝试使用VisualStateManager,但我不确定它是如何工作的,我还没有将工作的视觉状态应用于某些东西。
任何人都可以解释如何使用VisualStates,或者甚至建议一种替代方法吗?到目前为止,这是我的代码供参考:
<Application
x:Class="VSC_QC1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:VSC_QC1"
RequestedTheme="Light">
<Application.Resources>
<Style x:Key="ListBoxStyle" TargetType="ListBox">
<Setter Property="FontFamily" Value="Tahoma"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="BorderThickness" Value="2" />
</Style>
<Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#00b300" Offset="0" />
<GradientStop Color="#107028" Offset="1" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
</Application>
这是我的 ListBox 本身(在 MainPage.xaml 中)的代码,带有控件模板:
<ListBox
x:Name="LightSelector"
Grid.Column="2"
Grid.Row="0"
Width="300"
Style="{StaticResource ListBoxStyle}"
FontSize="30" Background="#FFC8C8C8"
VerticalAlignment="Top"
HorizontalAlignment="Center"
SelectionChanged="LightSelector_SelectionChanged">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<ListViewItemPresenter
PointerOverBackground="{ThemeResource SystemControlHighlightListLowBrush}"
SelectedBackground="{ThemeResource SystemControlHighlightListMediumBrush}"
SelectedPointerOverBackground="{ThemeResource SystemControlHighlightListMediumBrush}"
PressedBackground="{ThemeResource SystemControlHighlightListMediumBrush}"
SelectedPressedBackground="{ThemeResource SystemControlHighlightListMediumBrush}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBoxItem>White flood</ListBoxItem>
<ListBoxItem>UV flood</ListBoxItem>
<ListBoxItem>IR flood</ListBoxItem>
<ListBoxItem>White oblique</ListBoxItem>
<ListBoxItem>IR oblique</ListBoxItem>
<ListBoxItem>Coaxial</ListBoxItem>
<ListBoxItem>Backlight panel</ListBoxItem>
</ListBox>
【问题讨论】: