WPF 路由事件 根据点击的Button 打开对应的窗口
1 private void ButtonClick(object sender, RoutedEventArgs e) 2 { 3 // 路由事件,得到当前选中的Button 4 Button cmd = (Button)e.OriginalSource; 5 6 // 根据当前选中的Button的名字 7 // 打开窗口,注:xmal中Content为要打开窗口的名字 8 Type type = this.GetType(); 9 Assembly assembly = type.Assembly; 10 Window win = (Window)assembly.CreateInstance( 11 type.Namespace + "." + cmd.Content); 12 13 // Show the window. 14 win.ShowDialog(); 15 }
xmal:
<StackPanel Margin="10" ButtonBase.Click="ButtonClick">
<Button FontSize="20" Content="BindingDemo"/>
<Button FontSize="20" Content="XMLBinding"/>
<Button FontSize="20" Content="BindingValidationRules"/>
<Button FontSize="20" Content=""/>
<Button FontSize="20" Content=""/>
</StackPanel>
旧方式:
在对应的Button_Click事件
new实例
show实例