我有以下 ViewModel :
public class ViewModel : INotifyPropertyChanged {
public ObservableCollection<A> MyCollection { get; set; }
public RelayCommand Command1{ get; set; }
public ViewModel() {
Command1= new RelayCommand(Method1);
}
void Method1() {
...
}
}
这查看:
<UserControl x:Class="TemplateEditor.Views.View"
xmlns:vm="clr-namespace:TemplateEditor.ViewModels"
DataContext="{DynamicResource ViewModel}">
<UserControl.Resources>
<vm:ViewModel x:Key="ViewModel"/>
</UserControl.Resources>
...
<Button DataContext="{StaticResource FormIdBlockViewModel}" Content="qwe" Command="{Binding Command1}" />
...
</UserControl>
在这种情况下按钮工作正常,但我想将所有命令移动到某个文件中。所以,如果我创建 AppCommands.cs 文件:
public class AppCommands
{
private static RelayCommand Command1 { get; set; }
static AppCommands()
{
Command1 = new RelayCommand(Method1);
}
public static void Method1(object parameter)
{
....
}
}
另外,我更改查看:
...
xmlns:commands="clr-namespace:TemplateEditor.Common"
...
<UserControl.Resources>
<vm:ViewModel x:Key="ViewModel"/>
<commands:AppCommands x:Key="AppCommands"/>
...
<Button DataContext="{StaticResource AppCommands}" Content="qwe" Command="{Binding Command1}" />
该命令不起作用。
您能否建议如何对AppCommands.Commands
进行绑定?