0
votes

I've just discovered the power of the datatriggers and now Im trying to change the background of each items in my listbox according to a boolean property in the type I've bound.

This is the Style setting:

<Style x:Key="InvalidSettingStyle" TargetType="StackPanel">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=ValidSetting}" Value="false">
            <Setter Property="Background" Value="Red"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

This is the Datatemplate that I use:

<DataTemplate  x:Key="ComboBoxTemplate" DataType="self:ListBoxComboBoxElement">
    <StackPanel Orientation="Vertical" Style="{StaticResource InvalidSettingStyle}">
        <CheckBox IsChecked="{Binding IsActive}" Content="{Binding Content}"/>
        <ComboBox ItemsSource="{Binding Path= FirstComboBox.Items}"  
            SelectedItem="{Binding Path=FirstComboBox.SelectedRevitElement}" 
            SelectionChanged="ComboBox_SelectionChanged"/>            
    </StackPanel>
</DataTemplate>

I accidently ran it with the DataTrigger settings you can see above without setting the ValidSetting property to true before loading the form and they all turned out red. So there is some kind of connection, but I cant change the background while loaded.

All my other bindings work like a charm. The items are in a observablecollection bound to the Itemsource property of the Listbox.

Any ideas?

1
Please read stackoverflow.com/help/mcve for advice on how to present your question in a useful way. Without a minimal, complete code example, it is not possible to understand what your code is doing, never mind explain how you might fix it. Without seeing the code, one possible guess is that your data object isn't a DependencyObject nor implements INotifyPropertyChanged. I.e. it has no way to report changes in the ValidSetting property value. But it could be something completely different instead. Please improve the code example. - Peter Duniho
It sounds like you just need to implement the INotifyPropertyChanged interface in your ListBoxComboBoxElement class. - Sheridan
I've read that, but maybe I've missunderstood the use of ObservableCollection? I thought that by using that collection all my properties would inform on change? Or could it be that it does not hold for derived types? Such as in this case because the type specified in the observable collection is the base class of ListBoxComboBoxElement? However, I did not think that was the problem since the template makes use of information in the derived type in the combobox. - Erik83
@Erik83 So you have a ListBox bound to an observable collection. Is the data template that you have posted the one used for the items in the list box? In other words, each item in your list box appears as a check box with a combo box next to it? - Steven Rands

1 Answers

0
votes

It was INotifyPropertyChanged that was required. I was so sure that I didnt need it when I had an observablecollection.

Thanks for the help!