2017-07-28 49 views
0

我有2個按鈕..在開始時,其中一個啓用,另一個不是。然後我有2個這些按鈕的命令,我需要爲它們實現按鍵綁定。這些命令還應該替換按鈕的點擊方法。WPF:命令阻礙按鈕啓用

當第一個按鈕被點擊時,應該啓用第二個按鈕。但它不起作用。如果我刪除第二個按鈕的命令,並添加點擊方法,而不是它的工作原理..

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

CanExecute是定義,該按鈕是基於e.CanExecute的值被禁用或禁用的,因此設置IsEnabled顯式地沒有效果,因爲它被設置爲隱式LY。

您可以引入一個由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; 
    } 
} 

無論如何,你可以完全的的Elid CanExecute -action和剛剛成立IsEnabled明確。這也將做到這一點。

public partial class MainWindow : Window { 

    // elided 

    private void CommandBinding_Executed_1(object sender, ExecutedRoutedEventArgs e) { 
     button1.IsEnabled = true; 
    } 
} 
+0

我不能CanExecute的Elid並依靠按鈕的IsEnabled屬性,因爲這樣的命令可以通過按鍵的快捷執行。但是你是對的..我可以添加一個變量並將按鈕的IsEnabled屬性綁定到它。 –

+0

好的,我明白了。無論如何,我很高興能夠提供幫助。請讓我知道,如果它爲你工作;) –

+0

完美的作品..感謝您的幫助:) –

0

爲什麼Button.IsEnabledDoesNotWork?認爲它是在XAML中設置的

因爲Command覆蓋了IsEnabled功能。 enter image description here

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; 

     } 
    } 
+0

這不是我寫的東西嗎? –

+0

是的,我的小晚了,在編輯窗口:) – Ramankingdom