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

Commit 410d444

Browse files
author
gumme
committed
Fixed type resolve when pasting a custom control using StaticResource.
1 parent cd29a15 commit 410d444

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
@@ -253,6 +253,57 @@ public void PasteCustomControlUsingMixedTypes()
253253
"xmlns:Controls0=\"clr-namespace:ICSharpCode.WpfDesign.Tests.Designer;assembly=ICSharpCode.WpfDesign.Tests\"",
254254
"xmlns:sdtcontrols=\"http://sharpdevelop.net/WpfDesign/Tests/Controls\"");
255255
}
256+
257+
[Test]
258+
public void PasteCustomControlUsingStaticResource()
259+
{
260+
DesignItem grid = CreateGridContextWithDesignSurface("<Button/>");
261+
262+
DesignItemProperty resProp = grid.Properties.GetProperty("Resources");
263+
Assert.IsTrue(resProp.IsCollection);
264+
DesignItem exampleClassItem = grid.Services.Component.RegisterComponentForDesigner(new ExampleClass());
265+
exampleClassItem.Key = "res1";
266+
resProp.CollectionElements.Add(exampleClassItem);
267+
268+
DesignItem myButton = grid.Services.Component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.Controls.CustomButton());
269+
grid.Properties["Children"].CollectionElements.Add(myButton);
270+
271+
myButton.Properties[TextBox.TagProperty].SetValue(new StaticResourceExtension());
272+
myButton.Properties[TextBox.TagProperty].Value.Properties["ResourceKey"].SetValue("res1");
273+
274+
// Verify xaml document to be copied
275+
string expectedXaml = "<Grid.Resources>\n" +
276+
" <Controls0:ExampleClass x:Key=\"res1\" />\n" +
277+
"</Grid.Resources>\n" +
278+
"<Button />\n" +
279+
"<sdtcontrols:CustomButton Tag=\"{StaticResource ResourceKey=res1}\" />\n";
280+
281+
AssertGridDesignerOutput(expectedXaml, grid.Context,
282+
"xmlns:Controls0=\"clr-namespace:ICSharpCode.WpfDesign.Tests.Designer;assembly=ICSharpCode.WpfDesign.Tests\"",
283+
"xmlns:sdtcontrols=\"http://sharpdevelop.net/WpfDesign/Tests/Controls\"");
284+
285+
var xamlContext = grid.Context as XamlDesignContext;
286+
Assert.IsNotNull(xamlContext);
287+
xamlContext.XamlEditAction.Copy(new[] {myButton});
288+
289+
grid = CreateGridContextWithDesignSurface("<Button/>");
290+
291+
resProp = grid.Properties.GetProperty("Resources");
292+
Assert.IsTrue(resProp.IsCollection);
293+
exampleClassItem = grid.Services.Component.RegisterComponentForDesigner(new ExampleClass());
294+
exampleClassItem.Key = "res1";
295+
resProp.CollectionElements.Add(exampleClassItem);
296+
297+
xamlContext = grid.Context as XamlDesignContext;
298+
Assert.IsNotNull(xamlContext);
299+
var selection = grid.Services.Selection;
300+
selection.SetSelectedComponents(new[] {grid});
301+
xamlContext.XamlEditAction.Paste();
302+
303+
AssertGridDesignerOutput(expectedXaml, grid.Context,
304+
"xmlns:Controls0=\"clr-namespace:ICSharpCode.WpfDesign.Tests.Designer;assembly=ICSharpCode.WpfDesign.Tests\"",
305+
"xmlns:sdtcontrols=\"http://sharpdevelop.net/WpfDesign/Tests/Controls\"");
306+
}
256307
}
257308

258309
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)