@@ -38,36 +38,7 @@ public static bool CanPrint(XamlObject obj)
3838 return false ;
3939 }
4040
41- foreach ( var property in obj . Properties . Where ( ( prop ) => prop . IsSet ) )
42- {
43- var value = property . PropertyValue ;
44- if ( value is XamlTextValue )
45- continue ;
46- else
47- {
48- XamlObject xamlObject = value as XamlObject ;
49- if ( xamlObject == null || ! xamlObject . IsMarkupExtension )
50- return false ;
51- else {
52- var staticResource = xamlObject . Instance as System . Windows . StaticResourceExtension ;
53- if ( staticResource != null &&
54- staticResource . ResourceKey != null ) {
55- XamlObject parent = GetNonMarkupExtensionParent ( xamlObject ) ;
56-
57- if ( parent != null ) {
58- var parentLocalResource = parent . ServiceProvider . Resolver . FindLocalResource ( staticResource . ResourceKey ) ;
59-
60- // If resource with the specified key is declared locally on the same object as the StaticResource is being used the markup extension
61- // must be printed as element to find the resource, otherwise it will search from parent-parent and find none or another resource.
62- if ( parentLocalResource != null )
63- return false ;
64- }
65- }
66- }
67- }
68- }
69-
70- return true ;
41+ return CanPrint ( obj , false , GetNonMarkupExtensionParent ( obj ) ) ;
7142 }
7243
7344 /// <summary>
@@ -115,6 +86,28 @@ public static string Print(XamlObject obj)
11586 return sb . ToString ( ) ;
11687 }
11788
89+ private static bool CanPrint ( XamlObject obj , bool isNested , XamlObject nonMarkupExtensionParent )
90+ {
91+ if ( ( isNested || obj . ParentObject == nonMarkupExtensionParent ) && IsStaticResourceThatReferencesLocalResource ( obj , nonMarkupExtensionParent ) ) {
92+ return false ;
93+ }
94+
95+ foreach ( var property in obj . Properties . Where ( ( prop ) => prop . IsSet ) ) {
96+ var value = property . PropertyValue ;
97+ if ( value is XamlTextValue )
98+ continue ;
99+ else {
100+ var xamlObject = value as XamlObject ;
101+ if ( xamlObject == null || ! xamlObject . IsMarkupExtension )
102+ return false ;
103+ else
104+ return CanPrint ( xamlObject , true , nonMarkupExtensionParent ) ;
105+ }
106+ }
107+
108+ return true ;
109+ }
110+
118111 private static XamlObject GetNonMarkupExtensionParent ( XamlObject markupExtensionObject )
119112 {
120113 System . Diagnostics . Debug . Assert ( markupExtensionObject . IsMarkupExtension ) ;
@@ -125,5 +118,21 @@ private static XamlObject GetNonMarkupExtensionParent(XamlObject markupExtension
125118 }
126119 return obj ;
127120 }
121+
122+ private static bool IsStaticResourceThatReferencesLocalResource ( XamlObject obj , XamlObject nonMarkupExtensionParent )
123+ {
124+ var staticResource = obj . Instance as System . Windows . StaticResourceExtension ;
125+ if ( staticResource != null && staticResource . ResourceKey != null && nonMarkupExtensionParent != null ) {
126+
127+ var parentLocalResource = nonMarkupExtensionParent . ServiceProvider . Resolver . FindLocalResource ( staticResource . ResourceKey ) ;
128+
129+ // If resource with the specified key is declared locally on the same object as the StaticResource is being used the markup extension
130+ // must be printed as element to find the resource, otherwise it will search from parent-parent and find none or another resource.
131+ if ( parentLocalResource != null )
132+ return true ;
133+ }
134+
135+ return false ;
136+ }
128137 }
129138}
0 commit comments