Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9244891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:08:30+00:00 2026-06-18T09:08:30+00:00

<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

  • 0
<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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:08:31+00:00Added an answer on June 18, 2026 at 9:08 am

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

      <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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have simple window: <Window.Resources> <Style TargetType={x:Type TextBlock}> <Setter Property=Foreground Value=Red/> </Style> </Window.Resources> <StackPanel>
In <Window.Resources> I have defined following style: <Style x:Key=textBlockStyle TargetType=TextBlock> <Setter Property=Margin Value=5,0,5,0/> </Style>
A simple window: <Window x:Class=MyApp.MainWindow xmlns=... xmlns:x=...> <Window.Resources> <Style TargetType=Grid> <Setter Property=Margin Value=8/> </Style>
my xml is : <Window.Resources> <Style TargetType=ListViewItem> <Setter Property=HorizontalContentAlignment Value=Stretch /> </Style> </Window.Resources> <Grid
Here is the code : <Window.DataContext> <local:MainWindowViewModel /> </Window.DataContext> <Window.Resources> <Style x:Key=RadioToggleButtonStyle TargetType=RadioButton> <Setter
I have the following XAML/Control Template for a ListViewItem: <Window.Resources> <ControlTemplate x:Key=ItemTemplate TargetType=ListViewItem> <Border
I am using a ListView with an ItemTemplate like this: <Window.Resources> <DataTemplate x:Key=ItemTemplate> <WrapPanel
I want to have one Style DataTrigger inside my window.resources that can be used
Lets say I have this bit of code: <Window> <Window.Resources> <Color x:Key=MyColor A=255 R=152
i am just beginning WPF and wonder why this works <Window ...> <Window.Resources> <Style

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.