imasm / wpfkb

WPF Touch Screen Keyboard
MIT License
16 stars 8 forks source link

close keyboard at certain times #1

Closed john1726 closed 2 years ago

john1726 commented 6 years ago

There are times I want the keyboard to open, and other times I want it to not open. Could you please tell me, how could I do that with wpfkb?

Originally, this is what I used to get the keyboard to be present:

    <keyboard:FloatingTouchScreenKeyboard
        x:Name="Keyboard"
        IsOpen="True"
        Width="1215"
        Height="600"
        VerticalOffset="200"
        PlacementTarget="{Binding ElementName=LoginView}"
        Placement="Center"
        AreAnimationsEnabled="True"
    />

Presently, this is what I have on pages I want to use the keyboard (see How can I access a control in mvvm model ):

public class InspectionCommentViewModel : NotificationStepViewModel
{
    public IInspectionConfirmationView InspectionConfirmationView { get; set; }

    public override void OnNavigatingTo()
    {
        if(HasNotification) _currentText = NotificationComment;

        if (!_inspection.IsCompleted)
        {
            IsOpen = true;
        }

        // The problem is that this view is always null, so I can never show the keyboard
        if (InspectionConfirmationView == null) return;
        {
            InspectionConfirmationView.ShowFloatingKeyboard();
        }
    }
  }

public partial class InspectionComment : UserControl, IInspectionConfirmationView
 {
    private FloatingTouchScreenKeyboard _floatingTouchScreenKeyboard;

    public InspectionComment()
    {
        InitializeComponent();

       // The problem is that this DataContext is null, and if I create an instance here it is not the same instance of the InspectionCommentViewModel that gets called elsewhere in the app
        if (DataContext == null)
        {
            DataContext = ServiceLocator.Current.GetInstance<InspectionCommentViewModel>();
        }

        // access you VM by strategy of your framework or choice - this example is when you store your VM in View's DataContext
        (DataContext as InspectionCommentViewModel).InspectionConfirmationView = this as IInspectionConfirmationView;
     }

     public void ShowFloatingKeyboard()
      {
          _floatingTouchScreenKeyboard =
            new FloatingTouchScreenKeyboard()
            {
                IsOpen = true,
                Width = 1215,
                Height = 540,
                Placement = PlacementMode.Center,
                VerticalOffset = 200,
                AreAnimationsEnabled = true
            };

        Binding myBinding =
            new Binding
            {
                ElementName = "InspectionCommentView",
                Source = _floatingTouchScreenKeyboard,
                Path = new PropertyPath("PlacementTarget")
            };

        // Attach to a target property 
        this.SetBinding(FloatingTouchScreenKeyboard.PlacementTargetProperty, myBinding);
    }
 }

So far, I have been trying to follow this pattern to get access to open the keyboard a view, but the problem is that my DataContext is null in the constructor. Is there any way I can get this working without using Caliburn.Micro? The only way I can see how to do it would be to use Caliburn.Micro and call OnViewAttached.

Basically, what I want is to be able to open and close the keyboard from the viewmodel instead of just having it set to open all the time in the XAML. Any suggestions?