Ejemplo n.º 1
0
        private TextBox GenerateTextBox()
        {
            TextBox tb = new TextBox
            {
                TextAlignment = TextAlignment.Center,
                MaxLength     = 4,
                Width         = 40,
                Height        = 20
            };

            if (Application.Current != null)
            {
                tb.Style = (Style)Application.Current.TryFindResource("DarkTextBoxStyle");;
            }

            Binding binding = new Binding("Value")
            {
                Converter = new ToolSizeToIntConverter(),
                Mode      = BindingMode.TwoWay
            };

            tb.SetBinding(TextBox.TextProperty, binding);
            TextBoxFocusBehavior behavor = new TextBoxFocusBehavior
            {
                FillSize = true
            };

            Interaction.GetBehaviors(tb).Add(behavor);
            return(tb);
        }
Ejemplo n.º 2
0
        private static void OnFocusRecieved(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            var behaviors = Interaction.GetBehaviors(sender);

            // Remove the existing behavior instances
            foreach (var old in behaviors.OfType<TextBoxFocusBehavior>().ToArray())
                behaviors.Remove(old);

            if ((bool)args.NewValue)
            {
                // Creates a new behavior and attaches to the target
                var behavior = new TextBoxFocusBehavior();

                // Apply the behavior
                behaviors.Add(behavior);
            }
        }