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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:30:01+00:00 2026-05-26T02:30:01+00:00

I have a class inherited from ListBox and a custom ControlTemplate for the ListBoxItems.

  • 0

I have a class inherited from ListBox and a custom ControlTemplate for the ListBoxItems.
I want to change the ListBoxItems background if a condition is true. I tried to use DataTrigger for this. I don’t want to check the condition in the ListBoxItems context object, I want to check it in the inherited ListBox class.

The question is how can I bind in the ControlTemplate the Trigger to a ListBox property, when it needs to decide the right value for each ListBoxItem in runtime?

    <Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border Name="bd">
                        <TextBlock Name="lbl" Text="{Binding Path=DataChar}" FontWeight="ExtraBold" FontSize="15" Margin="5"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <DataTrigger Binding="{Binding RelativeSource={ RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBox}}, Path=IsSymbolExists}" Value="True">
                            <Setter TargetName="bd" Property="Background" Value="Yellow" />
                        </DataTrigger>
                    </ControlTemplate.Triggers>
                 </ControlTemplate>
             </Setter.Value>
         </Setter>
    </Style>


public class CustomListBox : ListBox
{
 ...
     public bool IsSymbolExists
     {
          if(/*condition is true for the ListBoxItem*/)
              return true;
          return false;
     }
}
  • 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-26T02:30:01+00:00Added an answer on May 26, 2026 at 2:30 am

    Ok firstly few suggestions…

    Does your custom listbox control have only new properties (IsSymbolExists etc.) and no real behavior. If so please declare them as Attached Properties

    Secondly when this value IsSymbolExists becomes true for a ListBox ALL its items will become highlighted individually by a yellow border. This doesnt look like a well thought UI behavior. Sorry if that comes to you as a bit harsh!

    Also from the binding perspective the DataChar property looks like a data context based property i.e.e coming from some model. If so then its binding has to be done through an ItemTemplate under ListBox and not in a TextBlock under ControlTemplate of a ListBoxItem. And for exactly the same reason, DataTrigger wont work correctly in ControlTemplate.

    They will work correctly in ItemTemplate.

    So to summarize, your code needs to be fixed this way…

    1. You can get rid of CustomListBox. Create a boolean attached property called MyListBoxBehavior.IsSymbolExists. Attach it to your ListBox.

    2. You should get rid of ListBoxItem‘s ControlTemplate.

    In ListBox take helpm from this… (this code wont compile as it is) 🙂

        <ListBox local:MyListBoxBehavior.IsSymbolExists="{Binding WhateverBoolean}"
                 ItemsSource="{Binding WhateverCollection}">
            <ListBox.ItemTemplate>
               <DataTemplate>
                  <Border>
                   <Border.Style>
                      <Style TargetType="{x:Type Border}">
                          <Style.Triggers>
                             <DataTrigger
                                    Binding="{Binding
                                                RelativeSource={RelativeSource
                                                   Mode=FindAncestor,
                                                     AncestorType={x:Type ListBox}},
                                            Path=local:MyListBoxBehavior.IsSymbolExists}"
                                    Value="True">
                                 <Setter Property="Background" Value="Yellow" />
                             </DataTrigger>
                          </Style.Triggers> 
                       </Style>
                    </Border.Style> 
                    <TextBlock Name="lbl"
                               Text="{Binding Path=DataChar}"
                               FontWeight="ExtraBold"
                               FontSize="15"
                               Margin="5"/>
                  </Border>
               </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my iPad App, I have one custom class inherited from UIButton. I am
I have a class is inherited from DataContext to use Linq. public class Context
i have an application class inherited from QtGui.QDialog. I have to reload show-function but
I have a parent class and child class (inherited from parent). In the child
I have QStringListModel QStringListModel* blocksModel = new QStringListModel(); And a class inherited from the
I have a winforms form which is inherited from another form. e.g. class StartForm
I have a VectorN class, and a Vector3 class inherited from VectorN (which can
I have a class that is inherited from an abstract base class. class CStateBase
I have a SplitViewController (iPad) and its leftViewController is a class inherited from UITableViewController;
I have one class DataThread inherited from Thread. I am using two DataThread objects

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.