Question

I have a number of Buttons like this

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

where the Content property of the Button is being passed as the CommandParameter. A single Command in the ViewModel handles all the Buttons and 'knows what to do' based on that parameter (think of a control panel / keyboard type app).

This makes for a lot of repeated XAML. How can I put this repeated code into the base style for a Button?

Was it helpful?

Solution

I believe this is not supported for Silverlight within WP7, in the same way that it was not supported in the browser-based Silverlight before SL4 (see the following workaround). You could do one of the following:

  1. Create a user control that encapsulates a Button with the bindings you have shown in your question.
  2. Sub-class Button, and create your bindings in the C# code of your subclass.

Personally, I would go for (2). This means you can still style your Buttons in the usual way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top