【发布时间】:2015-08-06 12:33:22
【问题描述】:
我正在开发一个 windows phone 8.1 应用程序,当我在列表框中选择一个项目时,我想更改文本的颜色。 我的xml:
<StackPanel Margin="0,0,0,10" VerticalAlignment="Bottom">
<ListBox toolkit:TiltEffect.IsTiltEnabled="True" Height="auto" x:Name="contactlist2" FontSize="36" Margin="0,5,10,5" Foreground="White" RenderTransformOrigin="0.5,0.5" SelectionChanged="contactlist2_SelectionChanged" ItemContainerStyle="{StaticResource ListBoxItemStyle1}" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock x:Name="Contact_no" Text="{Binding}" FontSize="30" FontFamily="/fonts/SKARPALT.TTF#SkarpaLT" TextAlignment="Left" Foreground="white" Margin="25,13,0,0" FontWeight="Bold" Width="300" MouseLeftButtonUp="Contact_no_MouseLeftButtonUp"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
我也尝试过设置项目容器,但没有成功:
<Style x:Key="ListBoxItemStyle1" TargetType="ListBoxItem">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border x:Name="LayoutRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
</VisualState>
<VisualState x:Name="MouseOver"/>
<VisualState x:Name="Disabled">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="LayoutRoot">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TransparentBrush}"/>
</ObjectAnimationUsingKeyFrames>
<DoubleAnimation Duration="0" To=".5" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ContentContainer"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected">
</VisualState>
<VisualState x:Name="Selected">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
<DiscreteObjectKeyFrame KeyTime="0" Value="Red"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
谁能告诉我代码有什么问题?谢谢
【问题讨论】:
-
这是 WPF 还是 Windows Phone 8.1?它们不是一回事。
-
@GlenThomas Windows phone silverlight 8.1 应用,下面给出的答案不起作用