在单击按钮之前,keyBinding无效

时间:2019-10-30 08:15:20

标签: c# wpf key-bindings

每次单击快捷方式时,都应添加一个用户控件。但是,当我关闭用户控件并尝试再次打开它时,快捷方式不起作用。焦点在其他地方。单击窗口上的某些按钮并单击快捷方式时,将添加用户控件。

UserControl 1:

<Window
    x:class="Class1"
    ...>    
    <Window.InputBindings>
        <KeyBinding Command="ButtonCLickCommand "
                    Modifiers="Control" Key="Q"/>
    </Window.InputBindings>
    <Grid x:Name="Grid">
        <Button Content = "OpenUserControl" Command="ButtonCLickCommand "/>
        <Button Content = "Temp"/>
    </Grid>
</Window>

CodeBehind:

public ICommand ProfileCommand { get; set; }
ProfileCommand = new RelayCommand(Button_Click());

public void Button_Click()
{            
     _control = new Class2();
     _control.CloseAction = CloseClass2;
     Panel.SetZIndex(_control, 10);
     _control.Width = 200;
     _control.Height = 200;
     Grid.Children.Add(_control);
}
public void CloseClass2(Class2 control)
{
     Grid.Children.Remove(control);            
}

UserControl 2:

<UserControl
    x:class="Class2"
    ...>    
    <StackPanel>
        <Button Content="x" Click="Button_Click" Height="30">
        <Rectangle Fill="Black" Height="70"/>
    </StackPanel>
</UserControl>

后面的代码:

namespace namespace1.UserControls
{
    /// <summary>
    /// Interaction logic for ProfileUserControl.xaml
    /// </summary>
    public partial class Class2 : UserControl
    {
        public Action<Class2> CloseAction;

        public Class2()
        {           
            InitializeComponent();
        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            CloseAction(this);
        }
    }
}

这是我的代码的概述。帮帮我,我是WPF的新手。 谢谢你给我的每一个答案。 :)

1 个答案:

答案 0 :(得分:0)

在您的KeyBinding中...您应该绑定到View模型中的命令 .... 为了更容易..它省略了视图模型...这是解决方案,请注意,我为简单起见做了一些调整:

在mainWindow.xaml中:

 <Window.InputBindings>
    <KeyBinding Command="{Binding ButtonClickCommand,RelativeSource{RelativeSource FindAncestor,AncestorType=Window}}" Modifiers="Control" Key="Q"/>
    </Window.InputBindings>

    <StackPanel x:Name="MainGrid">
    <Button Content = "OpenUserControl" Command="{Binding ButtonClickCommand,RelativeSource={RelativeSource FindAncestor,AncestorType=Window}}"/>
    </StackPanel>

这是背后的代码:

 public partial class MainWindow : Window
    {
    public RelayCommand ButtonClickCommand { get; set; }
    public MainWindow()
    {
    InitializeComponent();
    ButtonClickCommand= new RelayCommand(MyButtonClickExcute);
    }
    private void MyButtonClickExcute()
    {
    UserControl1 userControl1 = new UserControl1 {Width = 50, Height = 50,CloseAction = RemoveUserControlFromPanel };
    Panel.SetZIndex(userControl1, 10);
    MainGrid.Children.Add(userControl1);
    }
    public void RemoveUserControlFromPanel(UserControl1 userControl1)
    {
    MainGrid.Children.Remove(userControl1);
    }
    }

以及userControl.xaml:

<Grid>
<Border Background="Blue" CornerRadius="10" BorderThickness="15" BorderBrush="Green">
                <Button VerticalAlignment="Center" HorizontalAlignment="Center" Width="125" Height="75" Content="Close" Click="ButtonBase_OnClick"></Button>
</Border>
</Grid>

最后,后面是userControl的代码:

 public partial class UserControl1 : UserControl
    {
        public Action<UserControl1> CloseAction { get; set; }
        public UserControl1()
        {
            InitializeComponent();
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            CloseAction(this);
        }
    }