Reputation: 1493
Is it possible in silverlight
to assign a same command to a set of buttons in a grid through style? or assign it at a common place instead of repeating?
I mean something like using this -
<Style TargetType="Button">
<Setter Property="Command" Value="{Binding Command1}"/>
</Style>
instead of -
<Button Command="{Binding Command1}"/>
<Button Command="{Binding Command1}"/>
<Button Command="{Binding Command1}"/>
...
<Button Command="{Binding Command1}"/>
Upvotes: 0
Views: 61
Reputation: 6172
What you are looking for is the ItemsControl.
<ItemsControl x:Name="MyItemsControl" ItemsSource="{Binding MyButtonItems}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Command="{Binding Command1}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
The Binding
will be evaluated against each item, therefore you have several ways of providing the command instance:
Binding
to have it evaluated against the DataContext
of the ItemsControl
instead of against each item: {Binding Path=DataContext.Command1, ElementName=MyItemsControl}
Upvotes: 1
Reputation: 1258
It is possible. In a simple way you can add a Style to the Resources
and use it as a StaticResource
. Assuming that Command1
belongs to the ViewModel
you are binding to the Window
. If not, provide proper path to the Command1
to bind it properly.
<Window.Resources>
<Style x:Key="buttonCommandStyle" TargetType="Button">
<Setter Property="Command" Value="{Binding Path=DataContext.Command1}" />
</Style>
</Window.Resources>
Then use it as the Style
for your Button
<Button Style="{StaticResource buttonCommandStyle}" />
Upvotes: 1