WPF 使用ItemsControl控件 自定义动态加载按钮控件,并可以点击按钮将Content属性传入后台

前台代码:

   <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; }
    }

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值