在WPF中使用按钮启用和禁用的一种方法是使用RoutedCommands而不是click_events。有4个步骤:
1)为您正在使用的每个按钮分配静态资源,作为父UI元素的资源。例如,假设您有一个Grid作为这些按钮的父级:
<Grid>
<Grid.Resources>
<RoutedCommand x:Key="cmdButtonCommandName" />
</Grid.Resources>
...
</Grid>
2)在同一父节点下为每个Command定义一个CommandBinding。代码变成:
<Grid>
<Grid.Resources>
<RoutedCommand x:Key="cmdButtonCommandName" />
</Grid.Resources>
<Grid.CommandBindings>
<CommandBinding CanExecute="CmdButtonCommandName_CanExecute"
Command="{StaticResource cmdButtonCommandName}"
Executed="CmdButtonCommandName_Executed"/>
</Grid.CommandBindings>
...
</Grid>
3)将静态资源指定为所需按钮的命令:
<Button Content="Excel" Command="{StaticResource cmdButtonCommandName}"/>
4)在后面的代码中添加方法CmdButtonCommandName_CanExecute和CmdButtonCommandName_Executed
private void CmdButtonCommandName_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void CmdButtonCommandName_Executed(object sender, ExecutedRoutedEventArgs e)
{
...
}
在那里你可以设置e.CanExecute,它定义按钮是启用还是禁用...
那么,回答你的问题......如果你想在按下buttonB后解锁(启用)buttonA,你在方法buttonB_Executed的末尾设置一个布尔标志isButtonBpressed等于true,然后在你设置e的buttonA_CanExecute方法中。 CanExcute = isButtonBpressed;
一开始可能看起来很复杂,但如果你掌握了它,那就非常简单了。