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

Commit f76f245

Browse files
committed
Merge pull request #502 from gumme/WpfDesignerMarkupExtensionStaticResourceFix
Wpf Designer StaticResource fix
2 parents f06d1aa + 577eb9c commit f76f245

2 files changed

Lines changed: 67 additions & 30 deletions

File tree

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,34 @@ public void AddBindingWithStaticResourceWhereResourceOnSameElementAlt()
639639
AddBindingWithStaticResourceWhereResourceOnSameElement(true);
640640
}
641641

642+
[Test]
643+
public void AddStaticResourceWhereResourceOnSameElement()
644+
{
645+
DesignItem button = CreateCanvasContext("<Button/>");
646+
DesignItem canvas = button.Parent;
647+
648+
DesignItemProperty resProp = button.Properties.GetProperty("Resources");
649+
Assert.IsTrue(resProp.IsCollection);
650+
DesignItem exampleClassItem = canvas.Services.Component.RegisterComponentForDesigner(new ExampleClass());
651+
exampleClassItem.Key = "res1";
652+
resProp.CollectionElements.Add(exampleClassItem);
653+
654+
button.Properties["Tag"].SetValue(new StaticResourceExtension());
655+
button.Properties["Tag"].Value.Properties["ResourceKey"].SetValue("res1");
656+
657+
string expectedXaml = "<Button>\n" +
658+
" <Button.Resources>\n" +
659+
" <t:ExampleClass x:Key=\"res1\" />\n" +
660+
" </Button.Resources>\n" +
661+
" <Button.Tag>\n" +
662+
" <StaticResourceExtension ResourceKey=\"res1\" />\n" +
663+
" </Button.Tag>\n" +
664+
"</Button>";
665+
666+
AssertCanvasDesignerOutput(expectedXaml, button.Context);
667+
AssertLog("");
668+
}
669+
642670
[Test]
643671
public void AddBrushAsResource()
644672
{

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

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)