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#?
- C#
PoshWpf.XamlHelper.ConvertToXaml( System.Windows.Markup.XamlReader.Parse( "<StackPanel xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'> <TextBlock Text='{Binding FullName}' /> </StackPanel>" ));
- PowerShell
[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.
- using System;
- using System.Windows;
- using System.ComponentModel;
- using System.Windows.Markup;
- using System.Windows.Data;
- using System.Globalization;
- namespace PoshWpf
- {
- public class BindingConverter : ExpressionConverter
- {
- public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
- {
- }
- public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
- {
- {
- var bindingExpression = value as BindingExpression;
- if (bindingExpression == null)
- return bindingExpression.ParentBinding;
- }
- return base.ConvertTo(context, culture, value, destinationType);
- }
- }
- public static class XamlHelper
- {
- static XamlHelper()
- {
- }
- internal static void Register(Type T, Type TC)
- {
- TypeDescriptor.AddAttributes(T, attr);
- }
- public static string ConvertToXaml(object ui)
- {
- //this code need for right XML fomating
- settings.Indent = true;
- settings.OmitXmlDeclaration = true;
- var writer = System.Xml.XmlWriter.Create(outstr, settings);
- //this string need for turning on expression saving mode
- dsm.XamlWriterMode = System.Windows.Markup.XamlWriterMode.Expression;
- System.Windows.Markup.XamlWriter.Save(ui, dsm);
- return outstr.ToString();
- }
- }
- }
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.
PowerShell Code Repository