From 6252f3b5078b0ecd77b5529d78d6db3bad1b68e7 Mon Sep 17 00:00:00 2001 From: gumme Date: Wed, 14 May 2014 14:49:51 +0200 Subject: [PATCH] Added support for native types (string, int, double, etc). --- .../Tests/Designer/ModelTests.cs | 39 ++++++++++++++----- .../WpfDesign.XamlDom/Project/XamlDocument.cs | 9 +++-- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/Designer/ModelTests.cs b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/Designer/ModelTests.cs index 44d4611df9..b3867622d5 100644 --- a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/Designer/ModelTests.cs +++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/Designer/ModelTests.cs @@ -668,31 +668,52 @@ namespace ICSharpCode.WpfDesign.Tests.Designer } [Test] - public void AddStringAsResource() + public void AddNativeTypeAsResource(object component, string expectedXamlValue) { DesignItem textBlock = CreateCanvasContext(""); DesignItem canvas = textBlock.Parent; DesignItemProperty canvasResources = canvas.Properties.GetProperty("Resources"); - DesignItem str = canvas.Services.Component.RegisterComponentForDesigner("stringresource 1"); - str.Key = "str1"; + DesignItem componentItem = canvas.Services.Component.RegisterComponentForDesigner(component); + componentItem.Key = "res1"; Assert.IsTrue(canvasResources.IsCollection); - canvasResources.CollectionElements.Add(str); + canvasResources.CollectionElements.Add(componentItem); + + DesignItemProperty prop = textBlock.Properties[TextBlock.TagProperty]; + prop.SetValue(new StaticResourceExtension()); + prop.Value.Properties["ResourceKey"].SetValue("res1"); - textBlock.Properties[TextBlock.TextProperty].SetValue(new StaticResourceExtension()); - DesignItemProperty prop = textBlock.Properties[TextBlock.TextProperty]; - prop.Value.Properties["ResourceKey"].SetValue("str1"); + string typeName = component.GetType().Name; string expectedXaml = "\n" + - " stringresource 1\n" + + " " + expectedXamlValue + "\n" + "\n" + - ""; + ""; AssertCanvasDesignerOutput(expectedXaml, textBlock.Context, "xmlns:Controls0=\"clr-namespace:System;assembly=mscorlib\""); AssertLog(""); } + + [Test] + public void AddStringAsResource() + { + AddNativeTypeAsResource("stringresource 1", "stringresource 1"); + } + + [Test] + public void AddDoubleAsResource() + { + AddNativeTypeAsResource(0.0123456789d, "0.0123456789"); + } + + [Test] + public void AddInt32AsResource() + { + const int i = 123; + AddNativeTypeAsResource(i, "123"); + } } public class MyMultiConverter : IMultiValueConverter diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlDocument.cs b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlDocument.cs index 7a23eabac7..48ee85ec63 100644 --- a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlDocument.cs +++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlDocument.cs @@ -193,7 +193,7 @@ namespace ICSharpCode.WpfDesign.XamlDom XmlElement xml = _xmlDoc.CreateElement(prefix, elementType.Name, ns); - if (hasStringConverter && XamlObject.GetContentPropertyName(elementType) != null) { + if (hasStringConverter && (XamlObject.GetContentPropertyName(elementType) != null || IsNativeType(instance))) { xml.InnerText = c.ConvertToInvariantString(instance); } else if (instance is Brush && forProperty != null) { // TODO: this is a hacky fix, because Brush Editor doesn't // edit Design Items and so we have no XML, only the Brush @@ -214,8 +214,6 @@ namespace ICSharpCode.WpfDesign.XamlDom } } } - } else if (instance is string) { - xml.InnerText = (string)instance; } return new XamlObject(this, xml, elementType, instance); @@ -274,5 +272,10 @@ namespace ICSharpCode.WpfDesign.XamlDom return prefix; } + + bool IsNativeType(object instance) + { + return instance.GetType().Assembly == typeof(String).Assembly; + } } }