2010-01-28 160 views
1

我想將一個枚舉綁定到一個按鈕的CommandParameters。由於按鈕出現在ItemsControl中,因此這不能是靜態的。WPF綁定枚舉到命令參數

下面是DataTemplate中:

<DataTemplate> 
    <Button Command="{Binding MyCommand}" CommandParameters="{Binding MyEnumParameter}" Text="{Binding MyText}" /> 
</DataTemplate> 

我不知道我需要做的例外是不能枚舉轉換爲字符串。我的猜測是我需要一個ValueConverter來驗證這是否是唯一的途徑。

+0

對於任何誰也試圖通過枚舉作爲命令參數[這裏](http://stackoverflow.com/questions/359699/passing-an-enum-value-as-command-parameter-from-xaml )是SO後,包括解決方案的幾個變種。 – XAMlMAX 2014-08-16 20:24:23

回答

1

是的,使用ValueConverter是正確的選擇。 This thread有你可以使用的例子。