我有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;
}
}
}
答案 0 :(得分:0)
当定义CanExecute
时,根据e.CanExecute
的值启用或禁用按钮,因此设置IsEnabled
显式无效,因为它是隐式设置的。
你可以引入一个由CommandBinding_CanExecute
返回的变量 - 据我所知,这会产生预期的效果。
public partial class MainWindow : Window {
// other details elided
bool _enableButton1 = false;
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e) {
e.CanExecute = _enableButton1;
}
private void CommandBinding_Executed_1(object sender, ExecutedRoutedEventArgs e) {
enableButton1 = true;
}
}
无论如何,您可以完全忽略CanExecute
- 操作,只需明确设置IsEnabled
即可。这也可以解决问题。
public partial class MainWindow : Window {
// elided
private void CommandBinding_Executed_1(object sender, ExecutedRoutedEventArgs e) {
button1.IsEnabled = true;
}
}
答案 1 :(得分:0)
为什么Button.IsEnabledDoesNotWork?以为它是在XAML中设置
public partial class SampleWindow : Window
{
private bool _isButton1Enabled = false;
public SampleWindow()
{
InitializeComponent();
}
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Test");
}
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
try
{
e.CanExecute = _isButton1Enabled;
}
catch (Exception)
{
throw;
}
}
private void CommandBinding_Executed_1(object sender, ExecutedRoutedEventArgs e)
{
_isButton1Enabled = true;
}
}