Wpf – Strange behaviour in the WPF DataTemplate and property.

data-bindingdatatemplatewpf

I have a DataTemplate that displays buttons in a StackPanel. Whenever a user clicks a button, the button is supposed to glow. So I have written the necessary DataTrigger in the Template and a Boolean condition in the property that I'm binding too. Here are the details below:

<DataTemplate x:Key="ActionItemsTemplate" DataType="ActionItemViewModel">

        <ItemsControl IsTabStop="False" ItemsSource="{Binding}" Margin="6,2">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>                    
                    <StackPanel Orientation="Horizontal" />                        
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>

            <ItemsControl.ItemTemplate>
                <DataTemplate>

                    <Button x:Name="ActionButton" Command="{Binding Path=Command }" Content="{Binding Path=DisplayName}" Style="{DynamicResource HeaderButton}"/>
                    <!-- Set special values for Selected Item -->
                    <DataTemplate.Triggers>
                        <DataTrigger Binding="{Binding IsSelected}" Value="True">
                            <Setter TargetName="ActionButton" Property="Style"  Value="{DynamicResource MainWindowSelectedButton}"/>
                            <!--Command="{Binding Path=Command}" Content="{Binding Path=DisplayName}"-->
                        </DataTrigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </DataTemplate>

I have implemented the INotifyPropertyChanged interface and the Property ActionItems returns an ObservableCollection.

The Problem: When I change the ObservableCollection and call the INotifyPropertyChanged event, it is not reflected in the DataTemplate directly, but only altering the property. However if I re-assgn the entire object with itself, it works perfectly.

e.g

void Test1()
        {
           _commands[0].IsSelected = !_commands[0].IsSelected;               
            _commands[0] = _commands[0];      // Does not work If this line is commented out        
            ActionItems = _commands;
        }

What could the problem be?

Edit: I see that the problem could be with the DataBinding in this case.

I have arrived at a similar problem now where I have bound the IsExpanded property of the Expander control to a bool property inside a TabPanel. When I toggle the bool property, the value is changed behind, but not reflected in the display. However suppose I change tabs and come back, I see the change has taken place. Is this related to this problem here?

And again I wonder what the problem could be (narrowed it down a bit :))

Edit 2: Solution for second Problem: I found that the OnPropertyChangedEvent of the INotifyPropertyChanged Interface needs to be called whenever a programmatic update on the
IsExpanded property is updated. As for the Original Problem, this does not seem to be the case, and I'm still trying to figure out what is going wrong. 🙂

Best Answer

I'm led to believe that you cannot replace the collection, only alter its contents.