【问题标题】:Change text color of selected item in Listbox wpf更改列表框 wpf 中所选项目的文本颜色
【发布时间】: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 应用,下面给出的答案不起作用

标签: c# wpf xaml listbox


【解决方案1】:

您可以使用DataTrigger 将样式应用于TextBlock,以查找包含ListBoxItem 并检查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>

【讨论】:

    【解决方案2】:

    也许是一些严重的问题: 将“ContentControl”更改为顶部,将“VisualStateManager.VisualStateGroups”更改为地面。

    如果这不起作用(相对可能) 试试这个 ObjectAnimationUsingKeyFrames 和改进后的解决方案:

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

    这会对你有所帮助。

    您的解决方案不起作用的原因很简单。 ContentPresenter 没有属性“Foreground”。

    希望这会有所帮助。

    顺便说一句,对不起我的英语不好。 :)

    【讨论】:

      猜你喜欢
      • 2011-07-01
      • 2013-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      • 2021-05-24
      相关资源
      最近更新 更多