【发布时间】:2018-01-04 09:58:24
【问题描述】:
我有 2 个按钮.. 一开始,其中一个已启用,另一个未启用。然后我对这些按钮有 2 个命令,我需要为它们实现键绑定。这些命令也应该替换按钮的点击方法。
当第一个按钮被点击时,第二个按钮应该被启用。但它不起作用。如果我删除第二个按钮的命令并添加一个点击方法,它会起作用..
xaml:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<RoutedUICommand x:Key="first" />
<RoutedUICommand x:Key="second" />
</Window.Resources>
<Window.CommandBindings>
<CommandBinding Command="{StaticResource first}" Executed="CommandBinding_Executed_1" />
<CommandBinding Command="{StaticResource second}" CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed" />
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Key="F4" Command="{StaticResource first}" />
<KeyBinding Key="F5" Command="{StaticResource second}" />
</Window.InputBindings>
<Grid>
<Button Command="{StaticResource first}" x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="154,146,0,0" VerticalAlignment="Top" Width="75"/>
<Button Command="{StaticResource second}" IsEnabled="False" x:Name="button1" Content="Button" HorizontalAlignment="Left" Margin="312,146,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</Window>
xaml.cs
using System.Windows;
using System.Windows.Input;
namespace WpfApplication1 {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e) {
MessageBox.Show("Test");
}
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e) {
e.CanExecute = button1.IsEnabled;
}
private void CommandBinding_Executed_1(object sender, ExecutedRoutedEventArgs e) {
button1.IsEnabled = true;
}
}
}
【问题讨论】: