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

  • Home
  • SEARCH
  • 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 671091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:20:08+00:00 2026-05-14T00:20:08+00:00

[Original] I have a ListBox which has its ItemsSource (this is done in the

  • 0

[Original]
I have a ListBox which has its ItemsSource (this is done in the code behind on as the window is created) databound to an ObservableCollection. The ListBox then has the following DataTemplate assigned against the items:

usercontrol.xaml

<ListBox x:Name="communicatorListPhoneControls"
         ItemContainerStyle="{StaticResource templateForCalls}"/>

app.xaml

<Style x:Key="templateForCalls" TargetType="{x:Type ListBoxItem}">  
    <Setter Property="ContentTemplate" Value="{StaticResource templateRinging}"/>  
        <Style.Triggers>  
            <DataTrigger Binding="{Binding Path=hasBeenAnswered}" Value="True">  
                <Setter Property="ContentTemplate" Value="{StaticResource templateAnswered}"/>  
            </DataTrigger>  
        </Style.Triggers>  
    </Setter>
</Style>

When the ObservableCollection is updated with an object, this appears in the ListBox with the correct initial DataTemplate, however when the hasBeenAnswered property is set to true (when debugging i can see the collection is correct) the DataTrigger does not re-evaluate and then update the ListBox to use the correct DataTemplate.

I have implemented the INotifyPropertyChanged Event in my object, and if in the template is bound to a value, i can see the value update. Its just that the DataTrigger will not re-evaluate and change to the correct template.

I know the DataTrigger binding is correct because if i close the window and open it again, it will correctly apply the second datatemplate, because the hasBeenAnswered is set to true.

[edit 1]
Following on from comments made by Timores I’ve tried the following:

usercontrol.xaml

<ListBox x:Name="communicatorListPhoneControls"
         ItemTemplate="{StaticResource communicatorCallTemplate}"/>`  

app.xaml:

<DataTemplate x:Key="communicatorCallTemplate">
    <Label x:Name="test">Not answered</Label>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=hasBeenAnswered}" Value="True">
                <Setter TargetName="test" Property="Background" Value="Blue"/>
            </DataTrigger>    
        </DataTemplate.Triggers>
    </Label>
</DataTemplate>

What happens now is similar to the first example, when a call comes in the “Not answered” label shows (one per call that exists as this is a listbox – normally when the window loads there will be no calls), the call is then answered and the proptery hasBeenAnswered is set to true, yet the “Not Answered” remains the same. If i close the window, and re-open it again (with the active call still with the property hasBeenAnswered set to true) the background is then blue. So it would appear to me like the datatrigger is simply not being run, until the window is re-run.

  • 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-14T00:20:08+00:00Added an answer on May 14, 2026 at 12:20 am

    What seems strange to me in the example is that you are using an ItemContainerStyle instead of an ItemTemplate.

    ItemContainerStyle applies to the ListBoxItem that contains each element in your ItemsSource. The ListboxItem does not have an hasBeenAnswered property, so I don’t see how the binding could work.

    I suggest creating a DataTemplate for the data type in your list box and using triggers to make the same changes as in your templateAnswered style.

    Edit: after OP used the suggestion of the ItemTemplate.

    I tried to reproduce the example, and it works fine for me.
    Here is my XAML (please disregard style, this is just an example):

    Not answered

        <ListBox x:Name="communicatorListPhoneControls" 
                 ItemTemplate="{StaticResource communicatorCallTemplate}"/>
    
        <Button Margin="0,20,0,0" Click="OnToggleAnswer" Content="Toggle answer status" />
    </StackPanel>
    

    And in the code-behind:

    public partial class Window1 : Window {
    
        public Window1() {
            InitializeComponent();
    
            List<PhoneCall> lpc = new List<PhoneCall>()
            {new PhoneCall(), new PhoneCall(), new PhoneCall(), new PhoneCall()};
    
            communicatorListPhoneControls.ItemsSource = lpc;
        }
    
        private void OnToggleAnswer(object sender, RoutedEventArgs e) {
    
            object o = communicatorListPhoneControls.SelectedItem;
    
            if (o != null) {
    
                PhoneCall pc = (PhoneCall) o;
                pc.hasBeenAnswered = ! pc.hasBeenAnswered;
            }
        }
    }
    
    public class PhoneCall : INotifyPropertyChanged {
    
        private bool _answered;
    
    
        public bool hasBeenAnswered {
            get { return _answered;  }
            set {
                if (_answered != value) {
                    _answered = value;
                    FirePropertyChanged("hasBeenAnswered");
                }
            }
        }
    
        private void FirePropertyChanged(string propName) {
    
            if (PropertyChanged != null) {
    
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
    

    Could you try to reproduce this and compare with your code ?
    Note: the smallest error in the property name given to PropertyChanged could explain your behaviour. The trigger could be based on the right property, but the notification could have a misspelled name.

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

Sidebar

Related Questions

I have a ListBox which happily displays data using a code-behind MVVM object. However,
I have a service named WcfService2 (original i know) which has an IService.cs file
I have a list box control: <asp:ListBox runat=server id=lbox autoPostBack=true /> The code behind
Let's say I have some original text: here is some text that has a
I have a plugin where the original code is ... // when the DOM
I have a php script which saves the original image, then resizes it -
I have a Windows Forms application (C#) containing a ListBox into which I have
I have a listbox that is bound to a List<T> -- this is working
I have a ListBox which I put some files, if the file is not
I have simple SL user control. A listbox which shows all customers and on

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.