<UserControl x:Class="TestingModule.Views.StepsManager"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:dd="urn:gong-wpf-dragdrop"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
             xmlns:behaviors="clr-namespace:UIShare.Behaviors;assembly=UIShare"
             xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
             xmlns:prism="http://prismlibrary.com/"
             d:DesignHeight="450"
             d:DesignWidth="800"
             mc:Ignorable="d">
    <Grid>
        <GroupBox Header="{Binding Title}">
            <TabControl   SelectedIndex="{Binding SelectedTabIndex, Mode=TwoWay}">
                <i:Interaction.Behaviors>
                    <behaviors:TabControlSelectionChangedBehavior Command="{Binding TabSelectionChangedCommand}"
                                                                  CommandParameter="{Binding SelectedTabHeader}" />
                </i:Interaction.Behaviors>
                <TabItem Header="主程序">
                    <DataGrid dd:DragDrop.IsDragSource="{Binding Admin}"
                              dd:DragDrop.IsDropTarget="{Binding Admin}"
                              dd:DragDrop.UseDefaultDragAdorner="{Binding Admin}"
                              VirtualizingStackPanel.IsVirtualizing="True"
                              VirtualizingStackPanel.VirtualizationMode="Recycling"
                              VirtualizingStackPanel.ScrollUnit="Pixel"
                              AutoGenerateColumns="False"
                              Background="Transparent"
                              CanUserAddRows="False"
                              CanUserSortColumns="False"
                              ItemsSource="{Binding Program.StepCollection}"
                              SelectedItem="{Binding SelectedStep, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                              SelectionMode="Extended"
                              SelectionUnit="FullRow">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="SelectionChanged">
                                <i:InvokeCommandAction Command="{Binding SelectionChangedCommand}"
                                                       CommandParameter="{Binding SelectedItems, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                        <!--  行样式  -->
                        <DataGrid.RowStyle>
                            <Style TargetType="DataGridRow">
                                <Setter Property="Background"
                                        Value="Transparent" />
                                <Setter Property="Foreground"
                                        Value="Black" />
                                
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Result}"
                                                 Value="0">
                                        <Setter Property="Background"
                                                Value="DodgerBlue" />
                                        <Setter Property="Foreground"
                                                Value="White" />
                                    </DataTrigger>

                                    <DataTrigger Binding="{Binding Result}"
                                                 Value="1">
                                        <Setter Property="Background"
                                                Value="LimeGreen" />
                                        <Setter Property="Foreground"
                                                Value="White" />
                                    </DataTrigger>

                                    <DataTrigger Binding="{Binding Result}"
                                                 Value="2">
                                        <Setter Property="Background"
                                                Value="Red" />
                                        <Setter Property="Foreground"
                                                Value="White" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </DataGrid.RowStyle>

                        <!-- 列 -->
                        <DataGrid.Columns>
                            <DataGridCheckBoxColumn Width="58"
                                                    Binding="{Binding IsUsed, UpdateSourceTrigger=PropertyChanged}"
                                                    Header="启用" />
                            <DataGridTextColumn Binding="{Binding Index}"
                                                Header="序号"
                                                IsReadOnly="True" />
                            <DataGridTextColumn Binding="{Binding Name}"
                                                Header="名称"
                                                IsReadOnly="True" />
                            <DataGridTextColumn Binding="{Binding StepType}"
                                                Header="类型"
                                                IsReadOnly="True" />
                            <DataGridTextColumn Binding="{Binding Method.FullName}"
                                                Header="指令类型"
                                                IsReadOnly="True" />
                            <DataGridTextColumn Binding="{Binding Method.Name}"
                                                Header="指令"
                                                IsReadOnly="True" />
                            <DataGridTextColumn Binding="{Binding OKExpression}"
                                                Header="合格条件"
                                                IsReadOnly="True" />
                            <DataGridTextColumn Binding="{Binding RunTime}"
                                                Header="耗时(ms)"
                                                IsReadOnly="True" />
                            <DataGridTextColumn Binding="{Binding Result}"
                                                Header="结果"
                                                IsReadOnly="True" />
                            <DataGridTextColumn Binding="{Binding Description}"
                                                Header="备注" />
                        </DataGrid.Columns>

                        <!-- ContextMenu MVVM化 -->
                        <DataGrid.ContextMenu>
                            <ContextMenu>
                                <MenuItem Header="编辑"
                                          Command="{Binding EditStepCommand}" />
                                <MenuItem Header="复制"
                                          Command="{Binding CopyStepCommand}" />
                                <MenuItem Header="粘贴"
                                          Command="{Binding PasteStepCommand}" />
                                <MenuItem Header="删除"
                                          Foreground="Red"
                                          Command="{Binding DeleteStepCommand}" />
                            </ContextMenu>
                        </DataGrid.ContextMenu>



                    </DataGrid>
                </TabItem>
                <TabItem Header="错误程序">
                    <DataGrid dd:DragDrop.IsDragSource="True"
                              dd:DragDrop.IsDropTarget="True"
                              dd:DragDrop.UseDefaultDragAdorner="True"
                              AutoGenerateColumns="False"
                              Background="Transparent"
                              CanUserAddRows="False"
                              CanUserSortColumns="False"
                              ItemsSource="{Binding Program.ErrorStepCollection}"
                              SelectedItem="{Binding SelectedStep, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                              SelectionMode="Extended"
                              SelectionUnit="FullRow">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="SelectionChanged">
                                <i:InvokeCommandAction Command="{Binding SelectionChangedCommand}"
                                                       CommandParameter="{Binding SelectedItems, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                        <!--  行样式  -->
                        <DataGrid.RowStyle>
                            <Style TargetType="DataGridRow">
                                <Setter Property="Background"
                                        Value="Transparent" />
                                <Setter Property="Foreground"
                                        Value="Black" />

                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Result}"
                                                 Value="0">
                                        <Setter Property="Background"
                                                Value="DodgerBlue" />
                                        <Setter Property="Foreground"
                                                Value="White" />
                                    </DataTrigger>

                                    <DataTrigger Binding="{Binding Result}"
                                                 Value="1">
                                        <Setter Property="Background"
                                                Value="LimeGreen" />
                                        <Setter Property="Foreground"
                                                Value="White" />
                                    </DataTrigger>

                                    <DataTrigger Binding="{Binding Result}"
                                                 Value="2">
                                        <Setter Property="Background"
                                                Value="Red" />
                                        <Setter Property="Foreground"
                                                Value="White" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </DataGrid.RowStyle>

                        <!-- 列 -->
                        <DataGrid.Columns>
                            <DataGridCheckBoxColumn Width="58"
                                                    Binding="{Binding IsUsed, UpdateSourceTrigger=PropertyChanged}"
                                                    Header="启用" />
                            <DataGridTextColumn Binding="{Binding Index}"
                                                Header="序号"
                                                IsReadOnly="True" />
                            <DataGridTextColumn Binding="{Binding Name}"
                                                Header="名称"
                                                IsReadOnly="True" />
                            <DataGridTextColumn Binding="{Binding StepType}"
                                                Header="类型"
                                                IsReadOnly="True" />
                            <DataGridTextColumn Binding="{Binding Method.FullName}"
                                                Header="指令类型"
                                                IsReadOnly="True" />
                            <DataGridTextColumn Binding="{Binding Method.Name}"
                                                Header="指令"
                                                IsReadOnly="True" />
                            <DataGridTextColumn Binding="{Binding OKExpression}"
                                                Header="合格条件"
                                                IsReadOnly="True" />
                            <DataGridTextColumn Binding="{Binding RunTime}"
                                                Header="耗时(ms)"
                                                IsReadOnly="True" />
                            <DataGridTextColumn Binding="{Binding Result}"
                                                Header="结果"
                                                IsReadOnly="True" />
                            <DataGridTextColumn Binding="{Binding Description}"
                                                Header="备注" />
                        </DataGrid.Columns>

                        <!-- ContextMenu MVVM化 -->
                        <DataGrid.ContextMenu>
                            <ContextMenu>
                                <MenuItem Header="编辑"
                                          Command="{Binding EditStepCommand}" />
                                <MenuItem Header="复制"
                                          Command="{Binding CopyStepCommand}" />
                                <MenuItem Header="粘贴"
                                          Command="{Binding PasteStepCommand}" />
                                <MenuItem Header="删除"
                                          Foreground="Red"
                                          Command="{Binding DeleteStepCommand}" />
                            </ContextMenu>
                        </DataGrid.ContextMenu>

                        <!-- 键盘 删除键绑定 Delete 命令 -->
                        <!--<i:Interaction.Triggers>
    <i:EventTrigger EventName="PreviewKeyDown">
        <i:InvokeCommandAction Command="{Binding DeleteStepCommand}"
                               CommandParameter="{Binding SelectedStep}"
                               PassEventArgsToCommand="True" />
    </i:EventTrigger>
</i:Interaction.Triggers>-->

                    </DataGrid>
                </TabItem>
            </TabControl>
        </GroupBox>
        
    </Grid>
</UserControl>
