Home

XAML Support

edited June 2014
Hi,

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?

Regards

Oskar

: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 = "<TextBox Text=\"{Binding Name, Mode = OneWay}\" beh:DigitsOnlyBehavior.IsDigitOnly=\"True\" ></TextBox>"; 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)); } }

Comments

Sign In or Register to comment.