How to clone a datatemplate's triggers

Jan 14, 2010 at 12:16 PM
Edited Jan 15, 2010 at 3:28 PM



  I open a new discussion, since the issue is not limited to my original problem that was in .

  As show in the previous thread, I found a way to clone the template, and all was nice in my coding world, until the datatemplate was updated to have triggers. My cloning code runs fine but does not clone the datatriggers. As a consequence the cloned datatemplate do not have the proper layouting.

  I have made various try of enumerating the triggers collection of the source template to add them to the cloned template. All my try have been stopped by the same exception:

    - Unsupported exception unable to find child with name 'TextValue' in visual tree. However the visual tree is cloned and has the element in it.

  I cant' find any sample for properly creating datatriggers from code.

  Any help is welcome.


Jan 14, 2010 at 3:06 PM

Solved my own problem after figuring out that the clonelocalproperty method would clone the name property.

The following code Hammers the name property in the clone and fixes my issue. I can't see any reason why the name property would not set and not raise an exception or an error for not doing what was asked.

Private Shared Sub CloneObjectLocalProperties(ByRef O As FrameworkElementFactory, ByVal Source As DependencyObject, ByVal SourcePath As String, ByVal DestPath As String)
Dim E = Source.GetLocalValueEnumerator
While E.MoveNext

If TypeOf E.Current.Value Is BindingExpression Then
Dim B As Binding

B = CType(E.Current.Value, BindingExpression).ParentBinding

If B.Path.Path.Contains(SourcePath) Then
B = CType(CloneUsingXaml(CType(E.Current.Value, BindingExpression).ParentBinding, SourcePath, DestPath), Binding)
'With B
' .Path.Path = DestPath
'End With
End If

O.SetBinding(E.Current.Property, B)
If E.Current.Property.Name = "Name" Then
O.Name = CStr(E.Current.Value)
End If
O.SetValue(E.Current.Property, E.Current.Value) 'Source.GetValue(E.Current.Property))

End If
End While

End Sub

Jan 15, 2010 at 3:27 PM

Well, lets' reopen the topic.

   After cloning my template the triggers where applied, and all was nice again. Until, I noticed that some of my values would not update properly. After much hair pulling I tried to comment out the datatriggers from the datatemplate xaml and the data is updated properly again. How can I get both the datatemplates and the data-binding cloned ?


   Any help is welcomed