Search code examples
wpfwpf-controlsuielement

Activate UIElement on textbox from code behind


I have a custom WPF keyboard that I use for input in my application

xmlns:WpfKb="clr-namespace:WpfKb.Controls;assembly=WpfKb"

And in the resourcedictionary

<WpfKb:OnScreenKeypad x:Key="OnScreenKeypad"/>

In my XAML-code I use the following code to activate:

                    <TextBox Name="Sensor1Yoffset" Width="100" Margin="5,0" Grid.Row="1" Grid.Column="2" Style="{DynamicResource PmEditTextBox}" Text="{Binding Path=SensorOffsetY1}" VerticalContentAlignment="Center"  HorizontalContentAlignment="Center"
                WpfKb:FloatingTouchScreenKeyboard.KeyboardType="{StaticResource OnScreenKeypad}" 
                            WpfKb:FloatingTouchScreenKeyboard.OpenOnFocus="False" 
                            WpfKb:FloatingTouchScreenKeyboard.OpenOnMouseClick="{Binding Path=UseOnScreenKeyboard}" 
                            WpfKb:FloatingTouchScreenKeyboard.Placement="Left" 
                            WpfKb:FloatingTouchScreenKeyboard.PlacementTarget="{Binding ElementName=TheGui}" Grid.ColumnSpan="1" />

Now I need to add this in the code behind, but don't know where ro start looking.

      TextBox ChannelName = new TextBox();
      ChannelName.Style = (MyView.TryFindResource("PmEditTextBox") as Style);
      ChannelName.Width = 300;

The FloatingTouchScreenKeyboardclass is defined as

public partial class FloatingTouchScreenKeyboard : System.Windows.Controls.Primitives.Popup
    {
        public static readonly DependencyProperty AreAnimationsEnabledProperty = DependencyProperty.Register("AreAnimationsEnabled", typeof(bool), typeof(FloatingTouchScreenKeyboard), new UIPropertyMetadata(true));
        public static readonly DependencyProperty IsAllowedToFadeProperty = DependencyProperty.Register("IsAllowedToFade", typeof(bool), typeof(FloatingTouchScreenKeyboard), new UIPropertyMetadata(true));
        public static readonly DependencyProperty IsDraggingProperty = DependencyProperty.Register("IsDragging", typeof(bool), typeof(FloatingTouchScreenKeyboard), new UIPropertyMetadata(false));
        public static readonly DependencyProperty IsDragHelperAllowedToHideProperty = DependencyProperty.Register("IsDragHelperAllowedToHide", typeof(bool), typeof(FloatingTouchScreenKeyboard), new UIPropertyMetadata(false));
        public static readonly DependencyProperty IsKeyboardShownProperty = DependencyProperty.Register("IsKeyboardShown", typeof(bool), typeof(FloatingTouchScreenKeyboard), new UIPropertyMetadata(true));
        public static readonly DependencyProperty MaximumKeyboardOpacityProperty = DependencyProperty.Register("MaximumKeyboardOpacity", typeof(double), typeof(FloatingTouchScreenKeyboard), new UIPropertyMetadata(0.9d));
        public static readonly DependencyProperty MinimumKeyboardOpacityProperty = DependencyProperty.Register("MinimumKeyboardOpacity", typeof(double), typeof(FloatingTouchScreenKeyboard), new UIPropertyMetadata(0.2d));
        public static readonly DependencyProperty KeyboardHideDelayProperty = DependencyProperty.Register("KeyboardHideDelay", typeof(double), typeof(FloatingTouchScreenKeyboard), new UIPropertyMetadata(5d));
        public static readonly DependencyProperty KeyboardHideAnimationDurationProperty = DependencyProperty.Register("KeyboardHideAnimationDuration", typeof(double), typeof(FloatingTouchScreenKeyboard), new UIPropertyMetadata(0.5d));
        public static readonly DependencyProperty KeyboardShowAnimationDurationProperty = DependencyProperty.Register("KeyboardShowAnimationDuration", typeof(double), typeof(FloatingTouchScreenKeyboard), new UIPropertyMetadata(0.5d));
        public static readonly DependencyProperty DeadZoneProperty = DependencyProperty.Register("DeadZone", typeof(double), typeof(FloatingTouchScreenKeyboard), new UIPropertyMetadata(5d));

Any suggestions would be appreciated.


Solution

  • Most of the attached properties you are setting are actually exposed by the floating keyboard's base class, Popup. The Popup class has setter methods you can use to set the properties from C# code, e.g.:

    Popup.SetPlacementTarget(ChannelName, TheGui)
    

    For the properties with bindings, use the target's SetBinding method:

    ChannelName.SetBinding(
        Popup.OpenOnMouseClickProperty,
        new Binding
        {
            Path = new PropertyPath("UseOnScreenKeyboard")
        });
    

    Looking at the WpfKb project, I do not see any definition for the KeyboardType property. Perhaps it existed in an earlier version, or maybe you are using a customized/forked version?