Sfoglia il codice sorgente

修改plc变量编辑和删除功能

user_lt 1 anno fa
parent
commit
a91404589c

+ 2 - 1
BlankApp1/BlankApp1/ViewModels/BasicConfigViewModel/AddDetailViewModel.cs

@@ -60,7 +60,7 @@ namespace PLCTool.ViewModels.BasicConfigViewModel
 
 
         #region idialog接口实现
-        public string Title { set; get; } = "弹出窗体";
+        public string Title { set; get; } = "新增PLC变量";
 
         public event Action<IDialogResult> RequestClose;
 
@@ -84,6 +84,7 @@ namespace PLCTool.ViewModels.BasicConfigViewModel
                 {
                     if(item!=null)
                     {
+                        Title = "修改PLC变量";
                         PLCItem = item.PlcItem;
                         PLCAddress = item.PlcAddress;
                         SelectPLCType = item.PlcAddType;

+ 67 - 4
BlankApp1/BlankApp1/ViewModels/BasicConfigViewModel/PLCConfigViewModel.cs

@@ -36,10 +36,65 @@ namespace PLCTool.ViewModels.BasicConfigViewModel
             QueryCommand = new DelegateCommand<object>(Query);
             AddCommand= new DelegateCommand<object>(Add);
             ExportCommand = new DelegateCommand<string>(Export);
+            EditCommand = new DelegateCommand<object>(Edit);
+            DeleteCommand = new DelegateCommand<object>(Delete);
             GetPLCConfig();
         }
 
-      
+        private void Edit(object obj)
+        {
+            int id = Convert.ToInt32(obj);
+            var findPlc = allPLCConfigList.FirstOrDefault(x => (x.Id ==id));
+            DialogParameters parm = new DialogParameters();
+            parm.Add("Key", findPlc);
+            //弹出详情对话框
+            _dialog.ShowDialog("AddDetailView", parm, async callback =>
+            {
+                if (callback.Result == ButtonResult.OK)
+                {
+                    BasPlcItemConfigDto returnValue = callback.Parameters.GetValue<BasPlcItemConfigDto>("ReturnValue");
+                    if (returnValue != null)
+                    {
+                        var plcCon = _mapper.Map<BasPlcItemConfigDto, bas_plc_item_config>(returnValue);
+                        var findPlc = allPLCConfigList.FirstOrDefault(x => (x.PlcAddress == returnValue.PlcAddress) || (x.PlcItem == returnValue.PlcAddress));
+                        if (findPlc != null)
+                        {
+                            MessageBox.Show("已有此PLC变量地址或名称,请更改名称!", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
+                            return;
+                        }
+                        //修改
+                        plcCon.id = id;
+                        bool isSucc = _iBasPlcItemConfigService.Edit(plcCon);
+                        if (isSucc)
+                        {
+                            //重新读取PLC
+                            GetPLCConfig();
+                            _logger.LogInformation($"修改PLC变量成功。变量名{PLCItem},地址{PLCAddr}");
+                            MessageBox.Show("修改PLC变量成功", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
+                        }
+                    }
+                }
+
+            });
+        }
+
+        private void Delete(object obj)
+        {
+            int id = Convert.ToInt32(obj);
+            MessageBoxResult boxResult = MessageBox.Show("确认删除此条数据?", "确认", MessageBoxButton.OKCancel, MessageBoxImage.Question);
+            if (boxResult == MessageBoxResult.OK)
+            {
+                var del = _iBasPlcItemConfigService.Delete(id);
+                if (del)
+                {
+                    MessageBox.Show("删除成功!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
+                    GetPLCConfig();
+                }
+
+            }
+        }
+
+
 
 
 
@@ -108,10 +163,9 @@ namespace PLCTool.ViewModels.BasicConfigViewModel
         /// <exception cref="NotImplementedException"></exception>
         private void Add(object obj)
         {
-            DialogParameters parm = new DialogParameters();
-            parm.Add("Key", "");
+           
             //弹出详情对话框
-            _dialog.ShowDialog("AddDetailView", parm, async callback =>
+            _dialog.ShowDialog("AddDetailView", async callback =>
             {
                 if (callback.Result == ButtonResult.OK)
                 {
@@ -185,6 +239,15 @@ namespace PLCTool.ViewModels.BasicConfigViewModel
         public DelegateCommand<object> QueryCommand { set; get; }
         public DelegateCommand<object> AddCommand { set; get; }
         public DelegateCommand<string> ExportCommand { set; get; }
+
+        /// <summary>
+        /// 表格删除
+        /// </summary>
+        public DelegateCommand<Object> DeleteCommand { set; get; }
+        /// <summary>
+        /// 表格编辑按钮
+        /// </summary>
+        public DelegateCommand<Object> EditCommand { set; get; }
         #endregion
 
         #region 数据绑定

+ 4 - 11
BlankApp1/BlankApp1/Views/BasicConfigView/AddDetailView.xaml

@@ -7,17 +7,17 @@
              xmlns:prism="http://prismlibrary.com/"
                xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
              Height="300" Width="400" BorderBrush="#CBCBCB" BorderThickness="1">
-    <prism:Dialog.WindowStyle>
+    <!--<prism:Dialog.WindowStyle>
         <Style TargetType="Window">
             <Setter Property="SizeToContent" Value="WidthAndHeight" />
             <Setter Property="ResizeMode" Value="NoResize" />
             <Setter Property="prism:Dialog.WindowStartupLocation" Value="CenterScreen" />
             <Setter Property="WindowStyle" Value="None" />
         </Style>
-    </prism:Dialog.WindowStyle>
+    </prism:Dialog.WindowStyle>-->
     <Grid>
         <Grid.RowDefinitions>
-            <RowDefinition Height="40"/>
+            <RowDefinition Height="20"/>
             <RowDefinition />
             <RowDefinition />
             <RowDefinition />
@@ -25,14 +25,7 @@
             <RowDefinition />
             <RowDefinition />
         </Grid.RowDefinitions>
-        <Grid Grid.Row="0"  Cursor="Hand" >
-            <StackPanel Orientation="Horizontal" Margin="10,5,0,0">
-                <TextBlock Text="新增条件" FontSize="15"/>
-            </StackPanel>
-            <StackPanel HorizontalAlignment="Right" Margin="10,0,0,0">
-                <Button    Command="{Binding CloseCommand}" Background="Transparent" Content="✕" BorderThickness="0"  Cursor="Hand" />
-            </StackPanel>
-        </Grid>
+       
         <StackPanel Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="Center">
             <TextBlock Text="PLC地址:"  Style="{StaticResource NormalTextBlockStyle}" Margin="10,0,5,0" TextAlignment="Right" Width="80"/>
             <TextBox  Height="28" Width="120" Text="{Binding PLCAddress}" >

+ 23 - 1
BlankApp1/BlankApp1/Views/BasicConfigView/PLCConfigView.xaml

@@ -52,7 +52,29 @@ ItemsSource="{Binding PLCItemList}" IsReadOnly="True"  Padding="0">
                 <DataGridTextColumn Header="PLC地址" Binding="{Binding PlcAddress}" CellStyle="{StaticResource MyDataGridCellStyle}"/>
                 <DataGridTextColumn Header="描述" Binding="{Binding Remark}" CellStyle="{StaticResource MyDataGridCellStyle}"/>
                 <DataGridTextColumn Header="数据类型" Binding="{Binding PlcAddType}" CellStyle="{StaticResource MyDataGridCellStyle}"/>
-               
+                <DataGridTemplateColumn Header="操作" Width="180" CellStyle="{StaticResource MyDataGridCellStyle}">
+                    <DataGridTemplateColumn.CellTemplate>
+                        <DataTemplate>
+                            <StackPanel Orientation="Horizontal">
+                                <Button  Width="auto" Background="Transparent" HorizontalContentAlignment ="Left" Foreground="Black" Height="25" BorderThickness="0"
+                                 Command="{Binding DataContext.EditCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=DataGrid}}" CommandParameter="{Binding Id}" Cursor="Hand" Margin="0,0,10,0" >
+
+                                    <StackPanel Orientation="Horizontal">
+                                        <TextBlock  Text="编辑" VerticalAlignment="Center" Foreground="Blue"/>
+                                    </StackPanel>
+                                </Button>
+                                <Button  Width="auto" Background="Transparent" HorizontalContentAlignment ="Left" Foreground="Black" Height="25" BorderThickness="0"
+                                 Command="{Binding DataContext.DeleteCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=DataGrid}}" CommandParameter="{Binding Id}" Cursor="Hand" Margin="0,0,10,0">
+
+                                    <StackPanel Orientation="Horizontal">
+                                        <TextBlock  Text="删除" VerticalAlignment="Center" Foreground="Blue"/>
+                                    </StackPanel>
+                                </Button>
+                                
+                            </StackPanel>
+                        </DataTemplate>
+                    </DataGridTemplateColumn.CellTemplate>
+                </DataGridTemplateColumn>
             </DataGrid.Columns>
 
         </DataGrid>