XAML Support

edited June 2014

I would like to use LINQPad for WPF-Behavior testing with XAML. The code below results in an error. I think it's a problem with namespace specification of the behavior. Does anyone know how to specify the namespace correctly?



:The CODE formatter doesn't show the code correctly. Some ";" are not shown correctly below!

void Main() { var context = new ParserContext(); context.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation"); context.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml"); context.XmlnsDictionary.Add("beh", "clr-namespace:LINQPad.UserQuery"); var xaml = ""; var element = (FrameworkElement)XamlReader.Parse(xaml, context); element.DataContext = new { Name = "Test" }; PanelManager.StackWpfElement(element); } // Define other methods and classes here public static class DigitsOnlyBehavior { public static bool GetIsDigitOnly(DependencyObject obj) { return (bool)obj.GetValue(IsDigitOnlyProperty); } public static void SetIsDigitOnly(DependencyObject obj, bool value) { obj.SetValue(IsDigitOnlyProperty, value); } public static readonly DependencyProperty IsDigitOnlyProperty = DependencyProperty.RegisterAttached("IsDigitOnly", typeof(bool), typeof(DigitsOnlyBehavior), new PropertyMetadata(false, OnIsDigitOnlyChanged)); private static void OnIsDigitOnlyChanged(object sender, DependencyPropertyChangedEventArgs e) { // ignoring error checking var textBox = (TextBox)sender; var isDigitOnly = (bool)(e.NewValue); if (isDigitOnly) textBox.PreviewTextInput += BlockNonDigitCharacters; else textBox.PreviewTextInput -= BlockNonDigitCharacters; } private static void BlockNonDigitCharacters(object sender, TextCompositionEventArgs e) { e.Handled = e.Text.Any(ch => !Char.IsDigit(ch)); } }


  • edited June 2014
    What exactly is the error you are getting?

    To get the most basic example working, you will need to add the following assemblies as a minimum:

    * PresentationCore.dll
    * PresentationFramework.dll
    * System.Xaml.dll
    * WindowsBase.dll

    Here is a simple, one line example:
    PanelManager.DisplayWpfElement(new TextBlock { Text = "LINQPad" });

    *Note: You will also need to add the System.Windows.Controls namespace to make sure the compiler can find the TextBlock class. Press the F4 key to bring up the Query Properties dialog where you can add assemblies on one tab, and namespaces on the other.
  • Thx for your reply. The Textbox works without the DigitsOnlyBehavior.
    The error: Line number "1" and line item "47" of "The unknown member '{clr-namespace: LINQPad.UserQuery DigitsOnlyBehavior.IsDigitOnly}" can not be set. ".

    I assume that the clr-namespace is specified incorrectly.

  • The problem is still existing. Has anyone an idea?
  • I have the same problem. I can't seem to find a namespace that will allow the XAML to bind to any functions.

    I've tried:


    and many reasonable variations.
  • same issue, going to see if I can figure out a work-around
  • if you reflect on a type defined in LINQPad, it appears the namespace is null. no idea how WPF is going to ever bind to that
  • You can trick LINQPad into giving types a namespace as follows:


    You will need to specify an assembly, too:

    context.XmlnsDictionary.Add ("beh", "clr-namespace:Foo;assembly=LINQPadQuery");

    To make this work, go to Edit | Preferences | Advanced > Execution, and set "Allow LINQPad to access internal types of other assemblies" to true. This will force LINQPad to always call the query assembly "LINQPadQuery" rather than giving it a unique name each time.
  • Thanks Mr. Albahari, but it seems all the types created in LINQPad are nested which Xaml won't bind to.
  • Add the following to the start of your query:
    #define NONEST
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!