<UserControl x:Class="BDU.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:local="clr-namespace:BDU.Views"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             d:DataContext="{d:DesignInstance Type=local:StepsManager}"
             d:DesignHeight="450"
             d:DesignWidth="800"
             mc:Ignorable="d">
    <Grid>
        <GroupBox Grid.Row="1"
                  Grid.Column="2"
                  Header="{Binding Title}">
            <DataGrid x:Name="ProgramDataGrid"
                      dd:DragDrop.IsDragSource="{Binding IsAdmin}"
                      dd:DragDrop.IsDropTarget="{Binding IsAdmin}"
                      dd:DragDrop.UseDefaultDragAdorner="{Binding IsAdmin}"
                      AutoGenerateColumns="False"
                      Background="Transparent"
                      CanUserAddRows="False"
                      CanUserSortColumns="False"
                      ItemsSource="{Binding Program.StepCollection}"
                      PreviewKeyDown="StepManager_KeyDown"
                      SelectionMode="Extended"
                      SelectionUnit="FullRow">
                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="断点"
                                            Width="Auto">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <!-- 条件判断，决定是否显示断点 -->
                                <Border Visibility="{Binding isBrokenpoint, Converter={StaticResource BooleanToVisibilityConverter}}"
                                        Background="Red"
                                        BorderThickness="1"
                                        Width="20"
                                        Height="20"
                                        CornerRadius="20"
                                        Padding="5">
                                    <TextBlock Text=""
                                               HorizontalAlignment="Center"
                                               VerticalAlignment="Center" />
                                </Border>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridCheckBoxColumn Width="58"
                                            Binding="{Binding IsUsed, UpdateSourceTrigger=PropertyChanged}"
                                            Header="启用" />
                    <DataGridTextColumn Binding="{Binding Index}"
                                        Header="序号"
                                        IsReadOnly="True" />
                    <DataGridTextColumn Binding="{Binding Name, UpdateSourceTrigger=PropertyChanged}"
                                        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, Converter={StaticResource StepResultToStringConverter}}"
                                        Header="结果"
                                        IsReadOnly="True" />
                    <DataGridTextColumn Binding="{Binding Description}"
                                        Header="备注" />
                </DataGrid.Columns>
                <!--  行样式定义  -->
                <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>
                            <DataTrigger Binding="{Binding Result}"
                                         Value="3">
                                <Setter Property="Background"
                                        Value="Red" />
                                <Setter Property="Foreground"
                                        Value="White" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.RowStyle>
                <DataGrid.ContextMenu>
                    <ContextMenu>
                        <MenuItem Click="StepEdit_Click"
                                  Header="编辑" />
                        <MenuItem Click="StepCopy_Click"
                                  Header="复制" />
                        <MenuItem Click="StepPaste_Click"
                                  Header="粘贴" />
                        <MenuItem Click="StepDelete_Click"
                                  Header="删除"
                                  Foreground="Red"/>
                        <MenuItem Click="SetOrCancelBroken_Click"
                                  Header="添加/取消断点"
                                  Foreground="Red"/>
                    </ContextMenu>
                </DataGrid.ContextMenu>
            </DataGrid>
        </GroupBox>
    </Grid>
</UserControl>
