【问题标题】:keyBinding doesn't works until a button is clicked在单击按钮之前,keyBinding 不起作用
【发布时间】:2019-10-30 08:15:20
【问题描述】:

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

用户控件 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>

代码隐藏:

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);            
}

用户控件 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 的新手。 感谢我得到的每一个答案。 :)

【问题讨论】:

  • 如何将事件处理程序附加到 Command 属性?不会报错吗?
  • 对不起,我只是给出了代码的大纲。我正在关注 MVVM,我不想添加视图模型代码。现在我已经添加了它

标签: c# wpf key-bindings


【解决方案1】:

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

在 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);
        }
    }

【讨论】:

  • 当用户控件关闭时,焦点不在主窗口上。所以事件(添加新的用户控件)不会被调用。为了将焦点带回主窗口,只有单击其他按钮,然后焦点才回到主窗口。如何改变焦点?.
  • 我不确定我是否完全理解您...从面板的子项中删除用户控制后,主窗口仍然处于活动状态...为什么会失去焦点?我上传了这个项目,所以你可以检查它:filehosting.org/file/details/829671/Wpf%20Test.rar
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-22
  • 2023-04-05
  • 1970-01-01
相关资源
最近更新 更多