Browse Source

Print short Syntax on MarkupExtensions (for Binding, Reference and StaticResource)

That means instead of {Binding Path=Property1} we Print: {Binding Property1}
pull/593/head
jogibear9988 11 years ago
parent
commit
6464e5b675
  1. 2
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/Designer/EditOperationTests.cs
  2. 10
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/Designer/ModelTests.cs
  3. 2
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/Designer/SetPropertyTests.cs
  4. 77
      src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/MarkupExtensionPrinter.cs

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

@ -291,7 +291,7 @@ namespace ICSharpCode.WpfDesign.Tests.Designer
" <Controls0:ExampleClass x:Key=\"res1\" />\n" + " <Controls0:ExampleClass x:Key=\"res1\" />\n" +
"</Grid.Resources>\n" + "</Grid.Resources>\n" +
"<Button />\n" + "<Button />\n" +
"<sdtcontrols:CustomButton Tag=\"{StaticResource ResourceKey=res1}\" />\n"; "<sdtcontrols:CustomButton Tag=\"{StaticResource res1}\" />\n";
AssertGridDesignerOutput(expectedXaml, grid.Context, AssertGridDesignerOutput(expectedXaml, grid.Context,
"xmlns:Controls0=\"clr-namespace:ICSharpCode.WpfDesign.Tests.Designer;assembly=ICSharpCode.WpfDesign.Tests\"", "xmlns:Controls0=\"clr-namespace:ICSharpCode.WpfDesign.Tests.Designer;assembly=ICSharpCode.WpfDesign.Tests\"",

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

@ -553,7 +553,7 @@ namespace ICSharpCode.WpfDesign.Tests.Designer
textBox.Properties[TextBox.TextProperty].Value.Properties["Path"].SetValue("SomeProperty"); textBox.Properties[TextBox.TextProperty].Value.Properties["Path"].SetValue("SomeProperty");
string expectedXaml = "<Button />\n" + string expectedXaml = "<Button />\n" +
"<TextBox Text=\"{Binding Path=SomeProperty}\" />\n"; "<TextBox Text=\"{Binding SomeProperty}\" />\n";
AssertCanvasDesignerOutput(expectedXaml, button.Context); AssertCanvasDesignerOutput(expectedXaml, button.Context);
AssertLog(""); AssertLog("");
@ -583,7 +583,7 @@ namespace ICSharpCode.WpfDesign.Tests.Designer
" <t:ExampleClass x:Key=\"bindingSource\" />\n" + " <t:ExampleClass x:Key=\"bindingSource\" />\n" +
"</Canvas.Resources>\n" + "</Canvas.Resources>\n" +
"<Button />\n" + "<Button />\n" +
"<TextBox Text=\"{Binding Path=StringProp, Source={StaticResource ResourceKey=bindingSource}}\" />"; "<TextBox Text=\"{Binding StringProp, Source={StaticResource bindingSource}}\" />";
AssertCanvasDesignerOutput(expectedXaml, button.Context); AssertCanvasDesignerOutput(expectedXaml, button.Context);
AssertLog(""); AssertLog("");
@ -620,7 +620,7 @@ namespace ICSharpCode.WpfDesign.Tests.Designer
" <TextBox.Resources>\n" + " <TextBox.Resources>\n" +
" <t:ExampleClass x:Key=\"bindingSource\" />\n" + " <t:ExampleClass x:Key=\"bindingSource\" />\n" +
" </TextBox.Resources>\n" + " </TextBox.Resources>\n" +
" <Binding Path=\"StringProp\" Source=\"{StaticResource ResourceKey=bindingSource}\" />\n" + " <Binding Path=\"StringProp\" Source=\"{StaticResource bindingSource}\" />\n" +
"</TextBox>"; "</TextBox>";
AssertCanvasDesignerOutput(expectedXaml, button.Context); AssertCanvasDesignerOutput(expectedXaml, button.Context);
@ -689,7 +689,7 @@ namespace ICSharpCode.WpfDesign.Tests.Designer
string expectedXaml = "<Canvas.Resources>\n" + string expectedXaml = "<Canvas.Resources>\n" +
" <SolidColorBrush x:Key=\"testBrush\" Color=\"#FFFF00FF\" />\n" + " <SolidColorBrush x:Key=\"testBrush\" Color=\"#FFFF00FF\" />\n" +
"</Canvas.Resources>\n" + "</Canvas.Resources>\n" +
"<CheckBox Foreground=\"{StaticResource ResourceKey=testBrush}\" />"; "<CheckBox Foreground=\"{StaticResource testBrush}\" />";
AssertCanvasDesignerOutput(expectedXaml, checkBox.Context); AssertCanvasDesignerOutput(expectedXaml, checkBox.Context);
AssertLog(""); AssertLog("");
@ -722,7 +722,7 @@ namespace ICSharpCode.WpfDesign.Tests.Designer
string expectedXaml = "<Canvas.Resources>\n" + string expectedXaml = "<Canvas.Resources>\n" +
" <" + typePrefix + typeName + " x:Key=\"res1\">" + expectedXamlValue + "</" + typePrefix + typeName + ">\n" + " <" + typePrefix + typeName + " x:Key=\"res1\">" + expectedXamlValue + "</" + typePrefix + typeName + ">\n" +
"</Canvas.Resources>\n" + "</Canvas.Resources>\n" +
"<TextBlock Tag=\"{StaticResource ResourceKey=res1}\" />"; "<TextBlock Tag=\"{StaticResource res1}\" />";
AssertCanvasDesignerOutput(expectedXaml, textBlock.Context, additionalXmlns); AssertCanvasDesignerOutput(expectedXaml, textBlock.Context, additionalXmlns);
AssertLog(""); AssertLog("");

2
src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/Designer/SetPropertyTests.cs

@ -44,7 +44,7 @@ namespace ICSharpCode.WpfDesign.Tests.Designer
button.Properties.GetProperty("Content").SetValue(new StaticResourceExtension()); button.Properties.GetProperty("Content").SetValue(new StaticResourceExtension());
button.Properties.GetProperty("Content").Value.Properties["ResourceKey"].SetValue("MyBrush"); button.Properties.GetProperty("Content").Value.Properties["ResourceKey"].SetValue("MyBrush");
// TODO : maybe we should support positional arguments from ctors as well => {StaticResource MyBrush}? // TODO : maybe we should support positional arguments from ctors as well => {StaticResource MyBrush}?
AssertCanvasDesignerOutput("<Button Width=\"100\" Height=\"200\" Content=\"{StaticResource ResourceKey=MyBrush}\" />", button.Context); AssertCanvasDesignerOutput("<Button Width=\"100\" Height=\"200\" Content=\"{StaticResource MyBrush}\" />", button.Context);
} }
[Test] [Test]

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

@ -20,6 +20,9 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;
namespace ICSharpCode.WpfDesign.XamlDom namespace ICSharpCode.WpfDesign.XamlDom
{ {
@ -34,7 +37,7 @@ namespace ICSharpCode.WpfDesign.XamlDom
public static bool CanPrint(XamlObject obj) public static bool CanPrint(XamlObject obj)
{ {
if (obj.ElementType == typeof(System.Windows.Data.MultiBinding) || if (obj.ElementType == typeof(System.Windows.Data.MultiBinding) ||
obj.ElementType == typeof(System.Windows.Data.PriorityBinding)) { obj.ElementType == typeof(System.Windows.Data.PriorityBinding)) {
return false; return false;
} }
@ -51,7 +54,37 @@ namespace ICSharpCode.WpfDesign.XamlDom
sb.Append(obj.GetNameForMarkupExtension()); sb.Append(obj.GetNameForMarkupExtension());
bool first = true; bool first = true;
foreach (var property in obj.Properties) { var properties = obj.Properties.ToList();
if (obj.ElementType == typeof(Binding)){
var p=obj.Properties.FirstOrDefault(x=>x.PropertyName=="Path");
if (p!=null && p.IsSet) {
sb.Append(" ");
AppendPropertyValue(sb, p.PropertyValue);
properties.Remove(p);
first = false;
}
}
else if (obj.ElementType == typeof(Reference)){
var p=obj.Properties.FirstOrDefault(x=>x.PropertyName=="Name");
if (p!=null && p.IsSet) {
sb.Append(" ");
AppendPropertyValue(sb, p.PropertyValue);
properties.Remove(p);
first = false;
}
}
else if (obj.ElementType == typeof(StaticResourceExtension)){
var p=obj.Properties.FirstOrDefault(x=>x.PropertyName=="ResourceKey");
if (p!=null && p.IsSet) {
sb.Append(" ");
AppendPropertyValue(sb, p.PropertyValue);
properties.Remove(p);
first = false;
}
}
foreach (var property in properties) {
if (!property.IsSet) continue; if (!property.IsSet) continue;
if (first) if (first)
@ -63,29 +96,33 @@ namespace ICSharpCode.WpfDesign.XamlDom
sb.Append(property.GetNameForMarkupExtension()); sb.Append(property.GetNameForMarkupExtension());
sb.Append("="); sb.Append("=");
var value = property.PropertyValue; AppendPropertyValue(sb, property.PropertyValue);
var textValue = value as XamlTextValue;
if (textValue != null) {
string text = textValue.Text;
bool containsSpace = text.Contains(' ');
if(containsSpace) {
sb.Append('\'');
}
sb.Append(text.Replace("\\", "\\\\"));
if(containsSpace) {
sb.Append('\'');
}
} else if (value is XamlObject) {
sb.Append(Print(value as XamlObject));
}
} }
sb.Append("}"); sb.Append("}");
return sb.ToString(); return sb.ToString();
} }
private static void AppendPropertyValue(StringBuilder sb, XamlPropertyValue value)
{
var textValue = value as XamlTextValue;
if (textValue != null) {
string text = textValue.Text;
bool containsSpace = text.Contains(' ');
if(containsSpace) {
sb.Append('\'');
}
sb.Append(text.Replace("\\", "\\\\"));
if(containsSpace) {
sb.Append('\'');
}
} else if (value is XamlObject) {
sb.Append(Print(value as XamlObject));
}
}
private static bool CanPrint(XamlObject obj, bool isNested, XamlObject nonMarkupExtensionParent) private static bool CanPrint(XamlObject obj, bool isNested, XamlObject nonMarkupExtensionParent)
{ {
if ((isNested || obj.ParentObject == nonMarkupExtensionParent) && IsStaticResourceThatReferencesLocalResource(obj, nonMarkupExtensionParent)) { if ((isNested || obj.ParentObject == nonMarkupExtensionParent) && IsStaticResourceThatReferencesLocalResource(obj, nonMarkupExtensionParent)) {

Loading…
Cancel
Save