2017-07-28 9 views
0

私は2つのボタンを持っています。最初は1つが有効で、もう1つは無効です。次に、私はそれらのためのキーバインディングを実装する必要があるこれらのボタンの2つのコマンドがあります。これらのコマンドは、ボタンのクリック方法を置き換える必要があります。WPF:有効にするコマンドヒンダリングボタン

最初のボタンをクリックすると、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; 
      } 
     } 
    } 

答えて

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 -actionをElideの可能性だけを明示的IsEnabledを設定します。これはトリックも同様に行います。

public partial class MainWindow : Window { 

    // elided 

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

、コマンドは、キーのショートカットで実行することができますので、私はCanExecuteをElideのボタンのIsEnabledプロパティに依存することはできません。しかし、あなたは正しいです。変数を追加し、ボタンのIsEnabledプロパティをそれにバインドできます。 –

+0

さて、わかりました。とにかく助けてくれてうれしい。それがあなたのために働いた場合私に教えてください;) –

+0

は完璧に働く..あなたの助けを感謝:) –

0

なぜButton.IsEnabledDoesNotWorkですか?コマンドがIsEnabled機能をオーバーライドするため、XAMLで

が設定されていると考えられました。 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

関連する問題