【问题标题】:How do I specify they style of a selected ListBoxItem in Windows Phone 8.1?如何在 Windows Phone 8.1 中指定选定 ListBox 项的样式?
【发布时间】:2014-07-16 14:58:50
【问题描述】:

目前,当我在列表中选择一个项目时,它会将项目设置为蓝色背景。我试图指定一个列表项应该没有背景,即当它被选中时是透明的。

到目前为止,我的 ListBox 定义为:

<ListBox Name="LogsListBox" Grid.Row="1" Background="Transparent">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="Background" Value="Transparent" />
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

但是当我选择一个列表项时它仍然显示为蓝色。

我需要做什么才能使列表项在被选中时透明?

【问题讨论】:

    标签: xaml windows-phone windows-phone-8.1


    【解决方案1】:

    您应该在项目中的某处定义一个名为“ListViewItemSelectedBackgroundThemeBrush”的画笔,如下所示:

    <SolidColorBrush x:Key="ListViewItemSelectedBackgroundThemeBrush" Color="#FF4617B4" />
    

    如果没有,请在您的应用程序资源字典中定义一个新的。

    【讨论】:

      【解决方案2】:

      我找到了这个答案,它奏效了:

      https://stackoverflow.com/a/7359024/181771

      基本上我把它粘贴到我的用户控件中:

      <UserControl.Resources>
          <Style 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" 
                                  Background="{TemplateBinding Background}" 
                                  HorizontalAlignment="{TemplateBinding HorizontalAlignment}" 
                                  VerticalAlignment="{TemplateBinding VerticalAlignment}" 
                                  BorderBrush="{TemplateBinding BorderBrush}" 
                                  BorderThickness="{TemplateBinding BorderThickness}">
                              <VisualStateManager.VisualStateGroups>
                                  <VisualStateGroup x:Name="CommonStates">
                                      <VisualState x:Name="Normal"/>
                                      <VisualState x:Name="MouseOver" />
                                      <VisualState x:Name="Disabled">
                                          <Storyboard>
                                              <ObjectAnimationUsingKeyFrames 
                                                  Storyboard.TargetName="LayoutRoot" 
                                                  Storyboard.TargetProperty="Background">
                                                  <DiscreteObjectKeyFrame KeyTime="0" 
                                                                          Value="{StaticResource TransparentBrush}"/>
                                              </ObjectAnimationUsingKeyFrames>
                                              <DoubleAnimation 
                                                  Storyboard.TargetName="ContentContainer" 
                                                  Storyboard.TargetProperty="Opacity" Duration="0" To=".5" />
                                          </Storyboard>
                                      </VisualState>
                                  </VisualStateGroup>
                                  <VisualStateGroup x:Name="SelectionStates">
                                      <VisualState x:Name="Unselected"/>
                                      <VisualState x:Name="Selected">
                                          <Storyboard>
                                              <ObjectAnimationUsingKeyFrames 
                                                  Storyboard.TargetName="ContentContainer" 
                                                  Storyboard.TargetProperty="Foreground">
                                                  <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/>
                                              </ObjectAnimationUsingKeyFrames>
                                          </Storyboard>
                                      </VisualState>
                                  </VisualStateGroup>
                              </VisualStateManager.VisualStateGroups>
                              <ContentControl x:Name="ContentContainer" 
                                              VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" 
                                              HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"  
                                              Margin="{TemplateBinding Padding}" 
                                              Content="{TemplateBinding Content}" 
                                              ContentTemplate="{TemplateBinding ContentTemplate}" 
                                              Foreground="{TemplateBinding Foreground}" />
                          </Border>
                      </ControlTemplate>
                  </Setter.Value>
              </Setter>
          </Style>
      </UserControl.Resources>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-03
        • 2016-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多