如何在样式中为Command和CommandParameter指定绑定?

时间:2012-03-23 10:48:35

标签: .net silverlight windows-phone-7 xaml commandparameter

我有很多像这样的按钮

<Button Content="A" 
        Command="{Binding ClickCommand}" 
        CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}, 
                                   Path=Content}" />

其中Button的Content属性作为CommandParameter传递。 ViewModel中的单个Command处理所有按钮,并根据该参数“知道该做什么”(想想控制面板/键盘类型应用程序)。

这会产生大量重复的XAML。如何将重复的代码放入Button的基本样式?

1 个答案:

答案 0 :(得分:2)

我认为WP7中的Silverlight不支持这种方式,就像在SL4之前基于浏览器的Silverlight不支持它一样(参见the following workaround)。您可以执行以下操作之一:

  1. 使用您在问题中显示的绑定创建一个封装Button的用户控件。
  2. 子类Button,并在子类的C#代码中创建绑定。
  3. 就个人而言,我会选择(2)。这意味着您仍然可以按常规方式设置按钮的样式。