PoshCode Logo PowerShell Code Repository

WpfBindingHelper (modification of post by view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1710"></script>download | new post

I can’t get this to work from PowerShell, but it works fine from C#?

PoshWpf.XamlHelper.ConvertToXaml( System.Windows.Markup.XamlReader.Parse( "<StackPanel xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'> <TextBlock Text='{Binding FullName}' /> </StackPanel>" ));

[PoshWpf.XamlHelper]::ConvertToXaml( [System.Windows.Markup.XamlReader]::Parse( "<StackPanel xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'> <TextBlock Text='{Binding FullName}' /> </StackPanel>" ));

Both of them give back XAML, but in PowerShell … I’m missing the {Binding FullName} bit.

  1. using System;
  2. using System.Windows;
  3. using System.ComponentModel;
  4. using System.Windows.Markup;
  5. using System.Windows.Data;
  6. using System.Globalization;
  7.  
  8. namespace PoshWpf
  9. {
  10.    public class BindingConverter : ExpressionConverter
  11.    {
  12.       public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  13.       {
  14.          return (destinationType == typeof(MarkupExtension)) ? true : false;
  15.       }
  16.       public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  17.       {
  18.          if (destinationType == typeof(MarkupExtension))
  19.          {
  20.             var bindingExpression = value as BindingExpression;
  21.             if (bindingExpression == null)
  22.                throw new Exception();
  23.             return bindingExpression.ParentBinding;
  24.          }
  25.  
  26.          return base.ConvertTo(context, culture, value, destinationType);
  27.       }
  28.    }
  29.  
  30.    public static class XamlHelper
  31.    {
  32.       static XamlHelper()
  33.       {
  34.          Register(typeof(System.Windows.Data.BindingExpression), typeof(PoshWpf.BindingConverter));
  35.       }
  36.  
  37.       internal static void Register(Type T, Type TC)
  38.       {
  39.          var attr = new Attribute[] { new TypeConverterAttribute(TC) };
  40.          TypeDescriptor.AddAttributes(T, attr);
  41.       }
  42.  
  43.       public static string ConvertToXaml(object ui)
  44.       {
  45.  
  46.          var outstr = new System.Text.StringBuilder();
  47.          //this code need for right XML fomating
  48.          var settings = new System.Xml.XmlWriterSettings();
  49.          settings.Indent = true;
  50.          settings.OmitXmlDeclaration = true;
  51.  
  52.          var writer = System.Xml.XmlWriter.Create(outstr, settings);
  53.          var dsm = new System.Windows.Markup.XamlDesignerSerializationManager(writer);
  54.          //this string need for turning on expression saving mode
  55.          dsm.XamlWriterMode = System.Windows.Markup.XamlWriterMode.Expression;
  56.  
  57.          System.Windows.Markup.XamlWriter.Save(ui, dsm);
  58.          return outstr.ToString();
  59.       }
  60.    }
  61. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me