WPF中的事件处理程序

时间:2014-09-02 06:19:14

标签: wpf event-handling

为什么WPF中的所有事件处理程序都默认声明为私有访问?

private void CommonClickHandler(object sender, RoutedEventArgs e)

这是一种模式吗?

1 个答案:

答案 0 :(得分:1)

因为它们不应该被其他类使用,所以这是默认行为。

BTW:使用命令和System.Windows.Interactivity,一个像galasoft这样的框架,并遵循MVVM模式。

<UserControl.....>
<UserControl.DataContext>
   <local:YourViewModel/> <!-- Use a viewmodel locator instead -->
</UserControl.DataContext>
    <Button Content="Click Me" Command={Binding SomeCommand}/>
</UserControl>

VM:

public class YourViewModel : ViewModelBase 
{
   public ICommand SomeCommand{get; set;}

   public YourViewModel()
   {
       InitStuff();
   }

   protected virtual void InitStuff()
   {
      SomeCommand = new RelayCommand(ButtonClicked);
   }

   private void ButtonClicked() 
   {
     // DO stuff 
   }
}