如何更改列表框所选项目背景和前景?

时间:2020-12-15 15:05:25
<Window.Resources>
    <Style x:Key="ListBoxStyle" TargetType="{x:Type ListBox}">
        <Setter Property="Background" Value="{StaticResource ResourceKey=ListboxBack}"/>
        <Setter Property="Foreground" Value="Green"/>
        <Setter Property="Width" Value="284"/>
        <Setter Property="Height" Value="332"/>
        <Setter Property="Margin" Value="18,77,0,151"/>
        <Setter Property="ItemTemplate" Value="{DynamicResource DataTemplate1}"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="Padding" Value="0,0,0,0"/>
</Style>

    <DataTemplate x:Key="DataTemplate1">
        <Grid Width="276" Height="36" Background="{x:Null}" Opacity="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="0.069*"/>
                <ColumnDefinition Width="0.931*"/>
            </Grid.ColumnDefinitions>
            <TextBlock x:Name="recback" Padding="40,0,0,0" Text="{Binding [0], FallbackValue=Number}" Width="Auto" HorizontalAlignment="Stretch" Margin="-1.899,0,-5.334,0" Grid.Column="0" FontSize="13.333" Height="38.277" VerticalAlignment="Top" Foreground="Black" Background="{x:Null}" Opacity="1" Grid.ColumnSpan="2" />
            <Rectangle HorizontalAlignment="Stretch" Height="1" Margin="3.5,0" VerticalAlignment="Bottom" Width="Auto" Fill="White" Grid.ColumnSpan="2"/>
        </Grid>
    </DataTemplate>
</Window.Resources>

<ListBox Style="{StaticResource ResourceKey=ListBoxStyle}" BorderThickness="0" x:Name="listBox1" Foreground="White" FontSize="18" d:LayoutOverrides="VerticalAlignment" BorderBrush="{x:Null}" />

I create ListBox with DataTemplate. DataTemplate contains a Rectangle and a Textblock. When I select item in ListBox I want to change TextBlock foreground and Rectangle background. Could you help me?

我用DataTemplate创建ListBox。 DataTemplate包含一个Rectangle和一个Textblock。当我在ListBox中选择项目时,我想要更改TextBlock前景和矩形背景。你可以帮帮我吗?

1 个解决方案

#1


0  

Use a similar to the following approach. This way you will override the default Brushes with the specified x:Key used of this ListBox Only. Perhaps you need additional or different x:Keys to override

使用类似于以下方法。这样,您将使用此ListBox仅使用指定的x:Key覆盖默认Brushes。也许您需要额外的或不同的x:要覆盖的键

<ListBox>
    <ListBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue" />
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Green" />
    </ListBox.Resources>
</ListBox>

By reading again your question i understand that perhaps you also need DataTriggers in your DataTemplate. You might also try something like this Notice that Forground and BackGround should be set in the style not in TextBlock for this code to work:

通过再次阅读您的问题,我明白您可能还需要DataTemplate中的DataTriggers。您可能也尝试这样的事情请注意,Forground和BackGround应该以不在TextBlock中的样式设置,以使此代码工作:

  <TextBlock x:Name="recback" Padding="40,0,0,0" Text="{Binding [0], FallbackValue=Number}" Width="Auto" 
             HorizontalAlignment="Stretch" Margin="-1.899,0,-5.334,0" Grid.Column="0" FontSize="13.333" Height="38.277"
             VerticalAlignment="Top" Opacity="1" Grid.ColumnSpan="2">
      <TextBlock.Style>
          <Style TargetType="{x:Type TextBlock}">
              <Setter Property="Foreground" Value="Black"/>
              <Setter Property="Background" Value="{x:Null}"/>
              <Style.Triggers>
                  <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
                      <Setter Property="Foreground" Value="Red"/>
                      <Setter Property="Background" Value="Yellow"/>
                  </DataTrigger>
              </Style.Triggers>
          </Style>
      </TextBlock.Style>
  </TextBlock>

#1


0  

Use a similar to the following approach. This way you will override the default Brushes with the specified x:Key used of this ListBox Only. Perhaps you need additional or different x:Keys to override

使用类似于以下方法。这样,您将使用此ListBox仅使用指定的x:Key覆盖默认Brushes。也许您需要额外的或不同的x:要覆盖的键

<ListBox>
    <ListBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue" />
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Green" />
    </ListBox.Resources>
</ListBox>

By reading again your question i understand that perhaps you also need DataTriggers in your DataTemplate. You might also try something like this Notice that Forground and BackGround should be set in the style not in TextBlock for this code to work:

通过再次阅读您的问题,我明白您可能还需要DataTemplate中的DataTriggers。您可能也尝试这样的事情请注意,Forground和BackGround应该以不在TextBlock中的样式设置,以使此代码工作:

  <TextBlock x:Name="recback" Padding="40,0,0,0" Text="{Binding [0], FallbackValue=Number}" Width="Auto" 
             HorizontalAlignment="Stretch" Margin="-1.899,0,-5.334,0" Grid.Column="0" FontSize="13.333" Height="38.277"
             VerticalAlignment="Top" Opacity="1" Grid.ColumnSpan="2">
      <TextBlock.Style>
          <Style TargetType="{x:Type TextBlock}">
              <Setter Property="Foreground" Value="Black"/>
              <Setter Property="Background" Value="{x:Null}"/>
              <Style.Triggers>
                  <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
                      <Setter Property="Foreground" Value="Red"/>
                      <Setter Property="Background" Value="Yellow"/>
                  </DataTrigger>
              </Style.Triggers>
          </Style>
      </TextBlock.Style>
  </TextBlock>