views:

55

answers:

1

[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 observable collection. The List box then has the following data template 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>
</Style>

...

When the observable collection 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 data template.

I have implemented the INotifyPropertyChanged Event in my object, and if in the template i bind 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> </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 propery "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  A: 

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.

Timores
I've tried implementing this but its still only applying if i close the window and re-open it again. I will ammend the inital comment to show this. Do you have any ideas?
frozen
AHHHHH! I've got it, my own stupid spelling, you were correct, in the INotifyProperyChanged i had "hasBeenAsnwered" instead of "hasBeenAnswered". private Boolean _hasBeenAnswered; public Boolean hasBeenAnswered { get { return _hasBeenAnswered; } set { _hasBeenAnswered = value; NotifyPropertyChanged("hasBeenAnswered"); } } }thank you for all your help.
frozen
You're welcome. But I was wrong originally, i.e. the DataContext in the ItemContainerStyle is really the item inside, not the ListBoxItem. I changed my example to be more like your original question, and it works, too.
Timores