WPF MVVM显示隐藏的窗口并设置焦点

问题描述

我想显示/激活一个隐藏的窗口,并以编程方式将(keyboard-)焦点设置为文本框(如果可能,不违反mvvm)。

因此,如果用户按下热键(已实现),则会发生以下情况:

  • 窗口被显示+激活
  • 文本框获得焦点+键盘焦点(以便用户可以键入一些文本)

再次按热键应隐藏该窗口。

我尝试将alter tableint main(){ FILE *fp; fp = fopen("dict.txt","r"); char singleline[150],temp[150],temp_2[150]; int i,new; while (!feof(fp)) { i=0; new=0; fgets(singleline,150,fp); while (singleline[i] != '=') { temp[i] = singleline[i]; i++; } temp[i] = '\0'; printf("%s\n",temp); new = i+1; while (singleline[new] != '\0') { temp_2[new] = singleline[new]; new++; } temp_2[new] = '\0'; printf("%s\n",temp_2); } fclose(fp); return 0; } 绑定到一个布尔值,但是TwoWay-Binding遇到了一些问题。感谢您提供有关解决此问题的任何想法。

编辑:

MainWindow.xml:

Activated

ActivateBehavior.cs: https://stackoverflow.com/a/12254217/13215602

中使用的
Visibility

FocusExtension.cs:用于此https://stackoverflow.com/a/1356781/13215602

    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    </Window.Resources>

    <i:Interaction.Behaviors>
        <bh:ActivateBehavior Activated="{Binding Visible,Mode=TwoWay}"/>
    </i:Interaction.Behaviors>

    <Window.Visibility>
        <Binding Path="Visible" Mode="TwoWay" Converter="{StaticResource BoolToVisConverter}"/>
    </Window.Visibility>
    
    <TextBox Grid.Column="1" x:Name="SearchBar" bh:FocusExtension.IsFocused="{Binding Visible,Mode=TwoWay}"/>

Mainviewmodel.cs:

public class ActivateBehavior : Behavior<Window> {

  Boolean isActivated;

  public static readonly DependencyProperty ActivatedProperty =
    DependencyProperty.Register(
      "Activated",typeof(Boolean),typeof(ActivateBehavior),new PropertyMetadata(OnActivatedChanged)
    );

  public Boolean Activated {
    get { return (Boolean) GetValue(ActivatedProperty); }
    set { SetValue(ActivatedProperty,value); }
  }

  static void OnActivatedChanged(DependencyObject dependencyObject,DependencyPropertyChangedEventArgs e) {
    var behavior = (ActivateBehavior) dependencyObject;
    if (!behavior.Activated || behavior.isActivated)
      return;
    // The Activated property is set to true but the Activated event (tracked by the
    // isActivated field) hasn't been fired. Go ahead and activate the window.
    if (behavior.Associatedobject.WindowState == WindowState.Minimized)
      behavior.Associatedobject.WindowState = WindowState.normal;
    behavior.Associatedobject.Activate();
  }

  protected override void OnAttached() {
    Associatedobject.Activated += OnActivated;
    Associatedobject.Deactivated += OnDeactivated;
  }

  protected override void OnDetaching() {
    Associatedobject.Activated -= OnActivated;
    Associatedobject.Deactivated -= OnDeactivated;
  }

  void OnActivated(Object sender,EventArgs eventArgs) {
    this.isActivated = true;
    Activated = true;
  }

  void OnDeactivated(Object sender,EventArgs eventArgs) {
    this.isActivated = false;
    Activated = false;
  }

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)