2011-12-28 63 views
2

我定義了一個ICommand-Class ReadPersons,它使用特定的where-clause從數據庫讀取所有人員對象。如何將參數傳遞給ICommand?

通過按下按鈕執行該命令,並將where子句插入到文本框中。

問題:如何將文本從文本框傳遞到Execute-Command?

MainWindow.xaml:

<Button Command="{Binding ReadPersons}">Read persons</Button> 
    <TextBox Name="textBoxWhereClause" /> 

ReadPersons.cs:

public void Execute(object parameter) 
    { 
     // Read all persons with my where-clause 
     string whereClause = ??? //todo 
    } 

回答

3

當然可以。

<Button Command="{Binding ReadPersons}" 
     CommandParameter="{Binding Text, ElementName=textBoxWhereClause}"> 
      Read persons</Button> 
+0

GOTCHA!非常感謝你! – PeterP 2011-12-28 10:15:48

+0

將在其ICommand方法中寫入什麼來捕獲參數值? – 2016-03-10 09:10:00

0
<Button Command="{Binding ReadPersons}" CommandParameter="{Binding SomeProperty}" Content="Read Persons"/>