2016-08-15 2 views
0

ボタンをクリックしてメソッドを呼び出そうとしています。<i:Interaction.Triggers>が機能しないイベントへのコマンドのバインド

私はVisual Studio Community 2015を使用しています。Windows.Interactivityを取得するためにNuGet Package MangerでExpression Blendをインストールしました。

私のXAML:背後

<Window x:Class="TestApplication.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:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:TestApplication" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
    <Button Content="Button"> 
     <i:Interaction.Triggers> 
      <i:EventTrigger EventName="Click"> 
       <i:InvokeCommandAction Command="{Binding MessageBoxTest}"/> 
      </i:EventTrigger> 
     </i:Interaction.Triggers> 
    </Button> 
</Grid> 

マイコード:

namespace TestApplication 
{ 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      this.DataContext = this; 
     } 

     public void MessageBoxTest() 
     { 
      MessageBox.Show("Test Successful"); 
     } 
    } 
} 

を、私はボタンをクリックして、何も起こりません。

私は間違っていますか?

答えて

0

メソッドをバインドすることはできません。あなたがプロパティをバインドする必要があるので、それが好きですか:

private string _messageBoxTest; 

    public string MessageBoxTest 
    { 
     get{ return _messageBoxTest;} 
     set{ _messageBoxTest = value; 
      RaisePropertyChange(MessageBoxTest); 
      } 

    } 

はまた、あなたがINotifyPropertyChangedのインタフェースを実装する必要があります。

<Button Content="Button" 
Command="{Binding MessageBoxTest}"> 
    </Button> 
+0

ありがとうございました!私はバインディングがプロパティだけに制限されていたことを認識しませんでした。 –

0

古い質問を誰かが同じ問題を抱えていることができます。

あなたはただ単にInteractionTriggerを使用するように持っていけません。

イベント(Click event here)の後に何らかのアクション(MessageBoxTest here)を実行する場合は、コマンドで実行する必要があります。

私は通常をPrism library(私はC# routed commandが好きではありません)から使用します。

使用法は、このようなものです:

public DelegateCommand NameOfCommand = new DelegateCommand(() => MessageBoxTest()); 
関連する問題