设置 window 在 ICommands 中的位置不起作用
Setting the window's position in ICommands not working
我有一个简单的 WPF 应用程序,其顶部有一个菜单。我正在尝试将快捷方式绑定到其中一个菜单项,但我无法弄清楚。请注意,我这样做是出于业余爱好,而且对此还很陌生。
MainWindow.xaml
<Window.InputBindings>
<KeyBinding Gesture="Ctrl+R" Command="{Binding RepositionWindowCommand}" />
</Window.InputBindings>
<MenuItem Header="_Options">
<MenuItem x:Name="Menu_RepositionWindow" Header="_Reposition Window" Command="{Binding RepositionWindowCommand}" InputGestureText="Ctrl+R" />
</MenuItem>
MainMenu.cs
namespace WPF_Practice
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new PracticeDataContext();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
RepositionWindow();
}
public void RepositionWindow()
{
Left = SystemParameters.PrimaryScreenWidth - Width;
Top = 0;
}
}
public class PracticeDataContext
{
public ICommand RepositionWindowCommand { get; } = new RepositionWindowCommand();
}
public class RepositionWindowCommand : ICommand
{
public void Execute(object parameter)
{
var RepositionWindowClass = new MainWindow();
RepositionWindowClass.RepositionWindow();
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged { add { } remove { } }
}
}
这里的目标是当我点击菜单项,或者使用快捷键Ctrl+R时,将主要的window移动到右上角。我还尝试将 Top、Left 和 Width 放在 ICommand 中,以防万一解决了问题。
public class RepositionWindowCommand : ICommand
{
public void Execute(object parameter)
{
Left = SystemParameters.PrimaryScreenWidth - Width;
Top = 0;
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged { add { } remove { } }
}
它不识别 ICommand 中的 Left、Top 和 Width。它说 "The name does not exist in current context."
我在这里错过了什么?我如何实现这个目标?
当然,您不需要在 RepositionWindowCommand
中创建 RepositionWindowClass
。要在命令中访问 Window
实例,您应该使用 CommandParameter
绑定将其作为参数传递给命令。您还应该正确实施 CanExecuteChanged
事件
public class RepositionWindowCommand : ICommand
{
public void Execute(object parameter)
{
if (parameter is Window window)
{
window.Left = SystemParameters.PrimaryScreenWidth - window.Width;
window.Top = 0;
}
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
}
Xaml 用法
<MenuItem x:Name="Menu_RepositionWindow" Header="_Reposition Window" Command="{Binding RepositionWindowCommand}" InputGestureText="Ctrl+R" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
我有一个简单的 WPF 应用程序,其顶部有一个菜单。我正在尝试将快捷方式绑定到其中一个菜单项,但我无法弄清楚。请注意,我这样做是出于业余爱好,而且对此还很陌生。
MainWindow.xaml
<Window.InputBindings>
<KeyBinding Gesture="Ctrl+R" Command="{Binding RepositionWindowCommand}" />
</Window.InputBindings>
<MenuItem Header="_Options">
<MenuItem x:Name="Menu_RepositionWindow" Header="_Reposition Window" Command="{Binding RepositionWindowCommand}" InputGestureText="Ctrl+R" />
</MenuItem>
MainMenu.cs
namespace WPF_Practice
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new PracticeDataContext();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
RepositionWindow();
}
public void RepositionWindow()
{
Left = SystemParameters.PrimaryScreenWidth - Width;
Top = 0;
}
}
public class PracticeDataContext
{
public ICommand RepositionWindowCommand { get; } = new RepositionWindowCommand();
}
public class RepositionWindowCommand : ICommand
{
public void Execute(object parameter)
{
var RepositionWindowClass = new MainWindow();
RepositionWindowClass.RepositionWindow();
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged { add { } remove { } }
}
}
这里的目标是当我点击菜单项,或者使用快捷键Ctrl+R时,将主要的window移动到右上角。我还尝试将 Top、Left 和 Width 放在 ICommand 中,以防万一解决了问题。
public class RepositionWindowCommand : ICommand
{
public void Execute(object parameter)
{
Left = SystemParameters.PrimaryScreenWidth - Width;
Top = 0;
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged { add { } remove { } }
}
它不识别 ICommand 中的 Left、Top 和 Width。它说 "The name does not exist in current context."
我在这里错过了什么?我如何实现这个目标?
当然,您不需要在 RepositionWindowCommand
中创建 RepositionWindowClass
。要在命令中访问 Window
实例,您应该使用 CommandParameter
绑定将其作为参数传递给命令。您还应该正确实施 CanExecuteChanged
事件
public class RepositionWindowCommand : ICommand
{
public void Execute(object parameter)
{
if (parameter is Window window)
{
window.Left = SystemParameters.PrimaryScreenWidth - window.Width;
window.Top = 0;
}
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
}
Xaml 用法
<MenuItem x:Name="Menu_RepositionWindow" Header="_Reposition Window" Command="{Binding RepositionWindowCommand}" InputGestureText="Ctrl+R" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>