【发布时间】: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