在列表框wpf中更改所选项目的文本颜色

时间:2015-08-06 12:33:23

标签: c# wpf xaml listbox

我正在使用Windows Phone 8.1应用程序,当我选择列表框中的项目时,我想更改文本的颜色。 我的xaml:

<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>

有人可以告诉我代码有什么问题吗?谢谢

2 个答案:

答案 0 :(得分:0)

You can apply a style to the TextBlock with a DataTrigger that finds the containing ListBoxItem and checks IsSelected:

<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">
            <TextBlock.Style>
                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="Foreground" Value="Black"/>
                    <Style.Triggers>
                        <DataTrigger
                Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
                            <Setter Property="Foreground" Value="Red"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
    </DataTemplate>
</ListBox.ItemTemplate>

答案 1 :(得分:0)

也许有一些听觉问题: 将“ContentControl”更改为顶部,将“VisualStateManager.VisualStateGroups”更改为地面。

如果这不起作用(可能相对) 尝试使用改进的herachical的ObjectAnimationUsingKeyFrames解决方案:

<ObjectAnimationUsingKeyFrames 
                               Storyboard.TargetProperty="(TextElement.Foreground)"
                               Storyboard.TargetName="ContentContainer">
    <DiscreteObjectKeyFrame KeyTime="0" Value="Red"/>       
</ObjectAnimationUsingKeyFrames>

这对你有帮助。

您的解决方案无法正常工作的原因很简单。 ContentPresenter 没有属性“前景”。

希望这有帮助。

对不起我糟糕的英语。 :)