如何自定义在ListBox控件中选择的项目的视图?在我的情况下,我想定义所选项目的背景颜色。
谢谢!
答案 0 :(得分:0)
试试这个
private void listLocs_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBoxItem myitem = listLocs.SelectedItem as ListBoxItem;
SolidColorBrush brush = new SolidColorBrush(Color.FromArgb(255,255,0,0));
myitem.Background = brush;
}
更多细节来自: How can I change selected item's background color in Windows Phone?
答案 1 :(得分:0)
您可以通过使用ListBox的ItemContainerStyle属性设置ListBoxItem样式来实现此目的。使用以下步骤
因此,您的故事板将改为:
<VisualState x:Name="Selected">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
要:
<VisualState x:Name="Selected">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ContentContainer">
<DiscreteObjectKeyFrame KeyTime="0" Value="Red"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>