<UserControl x:Class="TestingModule.Views.ParametersManager"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
             xmlns:prism="http://prismlibrary.com/"
             xmlns:converters="clr-namespace:UIShare.Converters;assembly=UIShare"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             mc:Ignorable="d">
    <UserControl.Resources>
        <CollectionViewSource x:Key="VisibleParameters"
                              Source="{Binding Program.Parameters}"
                              IsLiveFilteringRequested="True">
            <CollectionViewSource.LiveFilteringProperties>
                <sys:String>IsVisible</sys:String>
            </CollectionViewSource.LiveFilteringProperties>
        </CollectionViewSource>
        <converters:ParameterCategoryToStringConverter x:Key="ParameterCategoryToStringConverter" />
        <converters:ParameterValueToStringConverter x:Key="ParameterValueToStringConverter" />
    </UserControl.Resources>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <i:InvokeCommandAction Command="{Binding LoadedCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <Grid>
        <GroupBox Header="设备/参数">
            <TabControl>
                <!-- 参数Tab -->
                <TabItem Header="参数">
                    <DataGrid Padding="10"
                              Background="Transparent"
                              ItemsSource="{Binding Source={StaticResource VisibleParameters}}"
                              AutoGenerateColumns="False"
                              CanUserAddRows="False"
                              SelectionMode="Extended"
                              SelectionUnit="FullRow"
                              SelectedItem="{Binding SelectedParameter, Mode=TwoWay}">

                        <DataGrid.Columns>
                            <DataGridTextColumn Header="类别"
                                                Binding="{Binding Category, Converter={StaticResource ParameterCategoryToStringConverter}}"
                                                IsReadOnly="True" />
                            <DataGridTextColumn Header="参数名"
                                                Binding="{Binding Name}"
                                                IsReadOnly="True" />
                            <DataGridTextColumn Header="类型"
                                                Binding="{Binding Type}"
                                                IsReadOnly="True" />
                            <DataGridTextColumn Header="值"
                                                MinWidth="20"
                                                Binding="{Binding Value, Converter={StaticResource ParameterValueToStringConverter}}"
                                                IsReadOnly="True" />
                        </DataGrid.Columns>

                        <DataGrid.ContextMenu>
                            <ContextMenu>
                                <MenuItem Header="新增"
                                          Command="{Binding ParameterAddCommand}" />
                                <MenuItem Header="编辑"
                                          Command="{Binding ParameterEditCommand}" />

                                <MenuItem Header="删除"
                                          Foreground="Red"
                                          Command="{Binding ParameterDeleteCommand}"/>
                            </ContextMenu>
                        </DataGrid.ContextMenu>

                    </DataGrid>
                </TabItem>

                <!-- 设备Tab -->
                <TabItem Header="设备">
                    <DataGrid Padding="10"
                              Background="Transparent"
                              ItemsSource="{Binding DeviceInfoVM}"
                              AutoGenerateColumns="False"
                              CanUserAddRows="False"
                              IsReadOnly="True"
                              SelectionMode="Extended"
                              SelectedItem="{Binding SelectedDevice, Mode=TwoWay}">

                        <DataGrid.Columns>
                            <DataGridTemplateColumn Header="设备状态">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <Ellipse Width="16"
                                                 Height="16"
                                                 StrokeThickness="1"
                                                 Stroke="DarkGray">
                                            <Ellipse.Style>
                                                <Style TargetType="Ellipse">
                                                    <Setter Property="Fill"
                                                            Value="Transparent" />
                                                    <Style.Triggers>
                                                        <DataTrigger Binding="{Binding IsConnected}" 
                                                                     Value="True">
                                                            <Setter Property="Fill"
                                                                    Value="LimeGreen" />
                                                        </DataTrigger>
                                                        <DataTrigger Binding="{Binding IsConnected}"
                                                                     Value="False">
                                                            <Setter Property="Fill"
                                                                    Value="Red" />
                                                        </DataTrigger>
                                                    </Style.Triggers>
                                                </Style>
                                            </Ellipse.Style>
                                        </Ellipse>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>

                            <DataGridTextColumn Header="设备名称"
                                                Binding="{Binding DeviceName}" />
                            <DataGridTemplateColumn Header="是否启用">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <CheckBox IsChecked="{Binding IsEnabled}" IsEnabled="False" />
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>

                        </DataGrid.Columns>
                        <DataGrid.ContextMenu>
                            <ContextMenu>
                                <MenuItem Header="编辑"
                                          Command="{Binding DeviceEditCommand}" />
                                <MenuItem Header="重新连接"
                                          Command="{Binding ReConnectCommand}" />
                                <MenuItem Header="关闭"
                                          Command="{Binding CloseCommand}" />
                            </ContextMenu>
                        </DataGrid.ContextMenu>
                    </DataGrid>
                </TabItem>
            </TabControl>
        </GroupBox>
    </Grid>
</UserControl>
