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 6794997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:17:23+00:00 2026-05-26T18:17:23+00:00

i got a DataTemplate for a listboxitem and i want to create a triger

  • 0

i got a DataTemplate for a listboxitem and i want to create a triger , so when a user click an item the background will change and also the label

my code:

<Window.Resources>
    <Style x:Key="RoundedItem" TargetType="ListBoxItem">
        <EventSetter Event="MouseDoubleClick" Handler="listViewItem_MouseDoubleClick" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border Name="ItemBorder" CornerRadius="10" BorderBrush="Black" BorderThickness="1" Margin="1" Background="Transparent">
                        <Label Name="ItemLabel" Foreground="Red" >
                            <ContentPresenter />
                        </Label>
                    </Border>

                        <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter TargetName="ItemBorder" Property="Background" Value="DeepSkyBlue" />
                            <Setter TargetName="ItemLabel" Property="Foreground" Value="Orange" />
                        </Trigger>

                    </ControlTemplate.Triggers>
                </ControlTemplate>

            </Setter.Value>
        </Setter>
    </Style>

    <DataTemplate x:Key="TitleTemplate" DataType="models:Title" >
        <StackPanel>
                <Image Source="{Binding ThumbFilePath}" Width="50" HorizontalAlignment="Center"/>
            <Label Content="{Binding Name}" HorizontalAlignment="Center" />
            <TextBlock Text="{Binding Description}" HorizontalAlignment="Center"  TextWrapping="Wrap" Padding="5,5,5,5"/>
        </StackPanel>
    </DataTemplate>

</Window.Resources>

What happend is that the TextBlock change his color and not the label..

anyone know why ?
Thanks.

  • 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-05-26T18:17:24+00:00Added an answer on May 26, 2026 at 6:17 pm

    The TextBlock inherits the Foreground definition from its parents in the visual tree. The Label, on the other hand, defines the Foreground in its default style.

    Your approach is “non-WPF-like” – you shouldn’t wrap the ContentPresenter in a Label control.

    The right approach depends on whether you want all text in the item to change its Foreground, or just the label?

    [In both cases, there’s no apparent benefit to using a Label in the data template – so I’ll assume that the label is changed to TextBlock.]

    If the answer to the above question is that all text should be changed: in the ControlTemplate of the ListBoxItem, in the trigger for IsSelected, from the seccond setter remove TargetName="ItemLabel" so the final setter is:

    <Setter Property="Foreground" Value="Orange" />
    

    This will change the foreground of the item that will affect the foreground of both TextBlocks in the data template.

    If you want to affect just one of the TextBlocks:

    1. remove the setter for the foreground from the control template
    2. add a trigger to your data template:
    
    <DataTemplate>
        <StackPanel>
            <Image .../>
            <TextBlock x:Name="Text01" ..../>
            <TextBlock x:Name="Text02" ..../>
        </StackPanel>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" Value="True">
                <Setter TargetName="Text01" Property="Foreground" Value="Orange"/>
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
    

    Side note: if you have to use Label control in your data template, bind its Foreground property to the Foreground of the list box item, like so:

    <Label Foreground="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"....../>
    

    If this doesn’t help, it means that your list box item inherits its foreground, so use:

    <Label Foreground="{Binding TextElement.Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"....../>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a user control that I'm using as my DataTemplate for all the
I've got some Xaml (WPF) that I'm trying to re-create in code. I think
I got a combobox in the grid's column: <ListView> <ListView.View> <GridView> <GridViewColumn> <GridViewColumn.CellTemplate> <DataTemplate>
Got this error message while trying to load view: The model item passed into
Got datatable with Id, parentId, description. It is a relationial table structure. I want
i got a listbox with items and i use datatemplate to display the items
I've got a View.xaml with the following set in Resources-section: <DataTemplate DataType={x:Type ViewModels:MyFirstViewModel}> <Views:MyFirstView
Ok so here's what I got, as example. <sdk:DataGridTemplateColumn> <sdk:DataGridTemplateColumn.CellTemplate> <DataTemplate> <HyperlinkButton Content=Delete Visibility={Binding
So I got a tabcontrol that is bound to an list(has name and code).
I've got many user controls like this: PageManageCustomers.xaml.cs: public partial class PageManageCustomers : BasePage

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.