Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 0563174

Browse files
author
gumme
committed
Fixed type resolve when pasting a custom control using StaticResource.
1 parent 1eed2fa commit 0563174

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/Designer/EditOperationTests.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,57 @@ public void PasteCustomControlUsingMixedTypes()
200200
"xmlns:Controls0=\"clr-namespace:ICSharpCode.WpfDesign.Tests.Designer;assembly=ICSharpCode.WpfDesign.Tests\"",
201201
"xmlns:sdtcontrols=\"http://sharpdevelop.net/WpfDesign/Tests/Controls\"");
202202
}
203+
204+
[Test]
205+
public void PasteCustomControlUsingStaticResource()
206+
{
207+
DesignItem grid = CreateGridContextWithDesignSurface("<Button/>");
208+
209+
DesignItemProperty resProp = grid.Properties.GetProperty("Resources");
210+
Assert.IsTrue(resProp.IsCollection);
211+
DesignItem exampleClassItem = grid.Services.Component.RegisterComponentForDesigner(new ExampleClass());
212+
exampleClassItem.Key = "res1";
213+
resProp.CollectionElements.Add(exampleClassItem);
214+
215+
DesignItem myButton = grid.Services.Component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.Controls.CustomButton());
216+
grid.Properties["Children"].CollectionElements.Add(myButton);
217+
218+
myButton.Properties[TextBox.TagProperty].SetValue(new StaticResourceExtension());
219+
myButton.Properties[TextBox.TagProperty].Value.Properties["ResourceKey"].SetValue("res1");
220+
221+
// Verify xaml document to be copied
222+
string expectedXaml = "<Grid.Resources>\n" +
223+
" <Controls0:ExampleClass x:Key=\"res1\" />\n" +
224+
"</Grid.Resources>\n" +
225+
"<Button />\n" +
226+
"<sdtcontrols:CustomButton Tag=\"{StaticResource ResourceKey=res1}\" />\n";
227+
228+
AssertGridDesignerOutput(expectedXaml, grid.Context,
229+
"xmlns:Controls0=\"clr-namespace:ICSharpCode.WpfDesign.Tests.Designer;assembly=ICSharpCode.WpfDesign.Tests\"",
230+
"xmlns:sdtcontrols=\"http://sharpdevelop.net/WpfDesign/Tests/Controls\"");
231+
232+
var xamlContext = grid.Context as XamlDesignContext;
233+
Assert.IsNotNull(xamlContext);
234+
xamlContext.XamlEditAction.Copy(new[] {myButton});
235+
236+
grid = CreateGridContextWithDesignSurface("<Button/>");
237+
238+
resProp = grid.Properties.GetProperty("Resources");
239+
Assert.IsTrue(resProp.IsCollection);
240+
exampleClassItem = grid.Services.Component.RegisterComponentForDesigner(new ExampleClass());
241+
exampleClassItem.Key = "res1";
242+
resProp.CollectionElements.Add(exampleClassItem);
243+
244+
xamlContext = grid.Context as XamlDesignContext;
245+
Assert.IsNotNull(xamlContext);
246+
var selection = grid.Services.Selection;
247+
selection.SetSelectedComponents(new[] {grid});
248+
xamlContext.XamlEditAction.Paste();
249+
250+
AssertGridDesignerOutput(expectedXaml, grid.Context,
251+
"xmlns:Controls0=\"clr-namespace:ICSharpCode.WpfDesign.Tests.Designer;assembly=ICSharpCode.WpfDesign.Tests\"",
252+
"xmlns:sdtcontrols=\"http://sharpdevelop.net/WpfDesign/Tests/Controls\"");
253+
}
203254
}
204255

205256
public class MyExtension : MarkupExtension

src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlTypeResolverProvider.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,14 @@ public Type Resolve(string typeName)
5757
typeNamespaceUri = GetNamespaceOfPrefix("");
5858
typeLocalName = typeName;
5959
}
60-
if (string.IsNullOrEmpty(typeNamespaceUri))
60+
if (string.IsNullOrEmpty(typeNamespaceUri)) {
61+
var documentResolver = this.document.RootElement.ServiceProvider.Resolver;
62+
if (documentResolver != null && documentResolver != this) {
63+
return documentResolver.Resolve(typeName);
64+
}
65+
6166
throw new XamlMarkupExtensionParseException("Unrecognized namespace prefix in type " + typeName);
67+
}
6268
return document.TypeFinder.GetType(typeNamespaceUri, typeLocalName);
6369
}
6470

0 commit comments

Comments
 (0)