我正在尝试在XAML中创建一个静态类中的静态函数的CommandBinding。编译器(VS)不接受,但说The member "CloseCanExecute" is not recognized or is not accessible.
然而,接受使用私有非静态成员,我也可以访问静态字符串(请参阅按钮)。
我做错了什么?
这是XAML
<Window x:Class="tt_WPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:tt_WPF"
Title="MainWindow" SizeToContent="WidthAndHeight">
<Window.CommandBindings>
<!-- THIS WILL NOT COMPILE -->
<CommandBinding Command="Close"
Executed="{x:Static local:XXX.CloseExecuted}" />
<!-- THIS IS WORKING
<CommandBinding Command="Close"
Executed="CloseExecuted" />
-->
</Window.CommandBindings>
<Button Content="{x:Static local:XXX.SomeStaticString}" />
</Window>
这是背后的代码:
public static class XXX
{
public static string SomeStaticString = "Hello World";
public static void CloseExecuted(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Hey, I'm closing.");
}
}
public partial class MainWindow : Window
{
private void CloseExecuted(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Hey, I'm closing.");
}
public MainWindow()
{
InitializeComponent();
}
}
答案 0 :(得分:2)
要总结这些评论, 无法通过X:L中的静态处理程序 绑定到静态处理程序,但您绝对可以通过代码隐藏。
在窗口加载事件或窗口构造函数中,您可以使用静态处理程序添加命令绑定。
this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Close,
XXX.CloseExecuted));