前台代码:
<ItemsControl ItemsSource="{Binding MyCommands}">
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type local:MyCommandWrapper}">
<Button Content="{Binding DisplayName}" Command="{Binding Command}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Content}"
Style="{StaticResource TextButton}"/>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
按钮样式代码:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="TextButton" TargetType="{x:Type Button}">
<Setter Property="Background" Value="{StaticResource ForegroundLighBrush}"/>
<Setter Property="Foreground" Value="{StaticResource ForegroundVeryDarkBrush}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="FontSize" Value="10"/>
<Setter Property="Margin" Value="10,5,10,5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ButtonBase}">
<Border x:Name="border"
CornerRadius="10"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="True">
<ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="border" Value="{StaticResource workBlueBrush}"/>
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" TargetName="border" Value="{StaticResource workBlueBrush}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" TargetName="border" Value="{StaticResource ForegroundDark}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
后台代码:
public class MainWindowView
{
public ICommand OpenCommand { get; private set; }
public ObservableCollection<MyCommandWrapper> _BaCommands = new ObservableCollection<MyCommandWrapper>();
public ObservableCollection<MyCommandWrapper> MyCommands
{
get
{
this._BaCommands = new ObservableCollection<MyCommandWrapper>() {
new MyCommandWrapper() { Command = OpenCommand, DisplayName = "101"},
new MyCommandWrapper() { Command = OpenCommand, DisplayName = "102"},
new MyCommandWrapper() { Command = OpenCommand, DisplayName = "103"},
new MyCommandWrapper() { Command = OpenCommand, DisplayName = "104" },
};
return this._BaCommands;
}
set
{
this._BaCommands = value;
RaisePropertyChanged("MyCommands");
}
}
public MainWindowView()
{
OpenCommand = new RelayCommand<object>(ExecuteButtonCommand);
}
private void ExecuteButtonCommand(object parameter)
{
var name = parameter.ToString();
}
}
public class MyCommandWrapper
{
public ICommand Command { get; set; }
public string DisplayName { get; set; }
}