ItemTemplateSelector's SelectTemplate method is not called when an object in a binded ObservableColl

  • Thread starter Thread starter Brendan Haddock
  • Start date Start date
B

Brendan Haddock

I am trying to create an ItemsControl which is bound to an
ObservableCollection using the ItemsSource property. I want to use a
different ItemTemplate for different items in the collection using the
ItemTemplateSelector. I've set this up, and it seems to be working
properly, except for one problem. If I edit an item in the
ObservableCollection, the ItemTemplate is not updated. My WPF application
does not call the SelectTemplate method of my ItemTemplateSelector when an
item in the collection is updated, only when a new item is added. I believe
the collection is being updated properly; the changes to the data show up in
the ItemsControl, the template just does not change. The items in my
collection implement INotifyPropertyChanged, so I believe the ItemsControl
is properly notified about data changes.

Is SelectTemplate supposed to be called when an item in a collection is
edited, or is this just a limitation in WPF? I am fairly new to C# and WPF,
so I could be doing something wrong. Please help me figure out what I'm
doing wrong, or let me know if WPF is designed to just call SelectTemplate
when an item is first added, and not when it is updated.

Thank you,
Brendan
 
Dear Brendan,

Thanks for using Microsoft Newsgroup Service. My name is Hongye Sun [MSFT]
and it is my pleasure to work with you on this issue.

* When will SelectTemplate be called *
Typically, SelectTemplate method of my ItemTemplateSelector will be called
in four situations in ItemsControl:
#1: The first time to show the content. (This is one special case of
situation #2)
#2: The content has been changed.
#3: The content template has been changed.
#4: The content template selector has been changed.

This issue is in the #2 situation. However, according to my research, when
an item in a collection is edited (The property of the item is changed),
the ItemsControl does not know its changes. It can only know the change
when the collection is changed. So when a new item is added or an existing
item is replaced by new one, the SelectTemplate method will be called.

For your better understanding, I wrote sample code here:

1. Edit the item property in a collection. The SelectTemplate method will
NOT be called.
ObservableCollection<Task> MyTodoList = new ObservableCollection<Task>();
InitializeMyTodoList();
MyTodoList[0].Priority = 1;

2. Edit the collection by replacing one item with new one. The
SelectTemplate method will be called.
MyTodoList[0] = new Task();

* Why the changes to the data show up in the ItemsControl *
You may be curious why the changes to the data show up in the ItemsControl.
Actually, it is not handled by ItemsControl, instead, it is handled by the
internal control which is defined in DataTemplate. For example in XAML code:

<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=TaskName}" />

The property "TaskName" is bound to TextBlock in a DataTemplate. When the
"TaskName" is changed, the notification event will notify to change the
Text property of the TextBlock control. Then, TextBlock control will
display the changed value by itself. It skips ItemsControl completely.

* Workarounds *
Based on the limitation of ItemsControl, we worked out two workarounds for
you.

#1: Copy the properties of the original item to a new item and set it back
to collection. E.g.
Tast task = new Task(MyTodoList[0]); // Implement a copy constructor first.
task.Priority = 1; // Change property
MyTodoList[0] = task; // Set back to collection

#2: Reset ItemTemplateSelector when you want to update the template. E.g.
MyTodoList[0].Priority = 1; // Set property
myItemsControl.ItemTemplateSelector = new TaskListDataTemplateSelector();
// Reset ItemTemplateSelector

From the performance perspective, both workarounds have performance
drawback to new instance of item or ItemTemplateSelector every time the
property is changed. #2 will have additional drawback to change the
templates for all the items.

Please let me know if they work for you. Thanks.

Regards,
Hongye Sun ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Brendan,

I have not heard from you for several days since my last reply. I am
writing check the status of the issue. Would you mind letting us know any
progress from your side.

In my last reply, I provided two workarounds for you. Are you able to find
suitable one for your business requirements?

Please feel free to let us know if you have any problem. I will be more
than happy to help. Thanks.

Regards,
Hongye Sun ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top