xaml:
<Window x:Class="WpfCommad.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Name="fdsafsd" Button.Click ="_stackPanel_Click_2">
<StackPanel x:Name="_stackPanel" Background="LightSlateGray" Button.Click ="_stackPanel_Click_1">
<StackPanel>
<Button x:Name="_btnClear" Height="100" Margin="5" Button.Click ="_stackPanel_Click_fd">
<Button x:Name="_btnClear1" Content="Cleiuiouioar" Height="28" Margin="5" Button.Click ="_stackPanel_Click_fd1111"/>
</Button>
</StackPanel>
<TextBox x:Name="_txtBox" Height="120"/>
</StackPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfCommad
{
/// <summary>
/// MainWindow.xaml 的互動邏輯
/// </summary>
public partial class MainWindow : Window
{ /// <summary>
/// 宣告并定義命令
/// </summary>
private RoutedCommand _clearCmd = new RoutedCommand("clear", typeof(MainWindow));
private RoutedCommand _clearCmd1 = new RoutedCommand("clear1", typeof(MainWindow));
public MainWindow()
{
InitializeComponent();
// 指定命令源與快捷鍵(輸入筆勢)
_btnClear.Command = _clearCmd;
_btnClear1.Command = _clearCmd1;
_clearCmd.InputGestures.Add(new KeyGesture(Key.C, ModifierKeys.Alt));
// 指定命令目標
_btnClear.CommandTarget = _txtBox;
_btnClear1.CommandTarget = _txtBox;
// 創建命令關聯
CommandBinding cb = new CommandBinding();
cb.Command = _clearCmd;
cb.CanExecute += cb_CanExecute;
cb.Executed += cb_Executed;
CommandBinding cb1 = new CommandBinding();
cb1.Command = _clearCmd1;
cb1.CanExecute += cb_CanExecute;
cb1.Executed += cb_Executed1;
// 命令關聯安置到外圍控制元件上
_stackPanel.CommandBindings.Add(cb);
_stackPanel.CommandBindings.Add(cb1);
}
void cb_Executed(object sender, ExecutedRoutedEventArgs e)
{
_txtBox.Clear();
MessageBox.Show("事件處理完成1");
// 執行完畢
e.Handled = true;
}
void cb_Executed1(object sender, ExecutedRoutedEventArgs e)
{
_txtBox.Clear();
MessageBox.Show("事件處理完成2");
// 執行完畢
e.Handled = true;
}
void cb_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
if (string.IsNullOrEmpty(_txtBox.Text))
{
e.CanExecute = false;
}
else
{
e.CanExecute = true;
}
e.Handled = true;
}
private void _stackPanel_Click_1(object sender, RoutedEventArgs e)
{
//MessageBox.Show("事件上升到了panel");
}
private void _stackPanel_Click_2(object sender, RoutedEventArgs e)
{
//MessageBox.Show("事件上升到了Window");
}
private void _stackPanel_Click_fd(object sender, RoutedEventArgs e)
{
MessageBox.Show("事件上升到了button");
}
private void _stackPanel_Click_fd1111(object sender, RoutedEventArgs e)
{
MessageBox.Show("事件上升到了button1");
}
}
}
我在此頁面加了兩個路由命令一個_clearCmd,一個_clearCmd1,目前想要實作_clearCmd1先執行,然后_clearCmd執行,看人說e.Handled = true可以阻止路由向上傳遞,我代碼中不加這句,為什么依然不想上傳遞命令?
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/213158.html
標籤:C#
上一篇:正則@的報錯
