From cad650b3d0d7ece9bb146dd48d290dbddf04fa22 Mon Sep 17 00:00:00 2001 From: Miguel Date: Tue, 2 Sep 2025 18:55:48 +0200 Subject: [PATCH] =?UTF-8?q?Mejorar=20la=20gesti=C3=B3n=20del=20foco=20en?= =?UTF-8?q?=20la=20interfaz,=20evitando=20interferencias=20con=20el=20PLCC?= =?UTF-8?q?ontrol.=20Se=20implementaron=20verificaciones=20para=20forzar?= =?UTF-8?q?=20actualizaciones=20de=20bindings=20sin=20robar=20el=20foco=20?= =?UTF-8?q?y=20se=20a=C3=B1adi=C3=B3=20un=20m=C3=A9todo=20para=20buscar=20?= =?UTF-8?q?controles=20padres=20en=20el=20=C3=A1rbol=20visual.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MainWindow.xaml.cs | 12 ++++---- ObjectManipulationManager.cs | 18 +++++++++++- ObjetosSim/UserControlFactory.cs | 48 +++++++++++++++++++++++++++++--- 3 files changed, 67 insertions(+), 11 deletions(-) diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 8bb6f04..84c49b7 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -387,8 +387,8 @@ namespace CtrEditor private void MainWindow_KeyDown(object sender, KeyEventArgs e) { - // Only force canvas focus if PanelEdicion doesn't have focus - if (!PanelEdicion.IsKeyboardFocusWithin) + // Only force canvas focus if PanelEdicion doesn't have focus AND PLCControl doesn't have focus + if (!PanelEdicion.IsKeyboardFocusWithin && !PLCSim.IsKeyboardFocusWithin) { if (!ImagenEnTrabajoCanvas.IsFocused) { @@ -819,8 +819,8 @@ namespace CtrEditor private void Canvas_KeyDown(object sender, KeyEventArgs e) { - // Only handle if PanelEdicion doesn't have focus - if (!PanelEdicion.IsKeyboardFocusWithin) + // Only handle if PanelEdicion doesn't have focus AND PLCControl doesn't have focus + if (!PanelEdicion.IsKeyboardFocusWithin && !PLCSim.IsKeyboardFocusWithin) { HandleKeyDown(e); } @@ -828,8 +828,8 @@ namespace CtrEditor private void ScrollViewer_PreviewKeyDown(object sender, KeyEventArgs e) { - // Only handle if PanelEdicion doesn't have focus - if (!PanelEdicion.IsKeyboardFocusWithin && + // Only handle if PanelEdicion doesn't have focus AND PLCControl doesn't have focus + if (!PanelEdicion.IsKeyboardFocusWithin && !PLCSim.IsKeyboardFocusWithin && (e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Up || e.Key == Key.Down)) { HandleKeyDown(e); diff --git a/ObjectManipulationManager.cs b/ObjectManipulationManager.cs index b2605d9..6dda638 100644 --- a/ObjectManipulationManager.cs +++ b/ObjectManipulationManager.cs @@ -7,6 +7,7 @@ using System.Windows.Input; using System.Windows.Media; using System.Windows.Shapes; using Color = System.Windows.Media.Color; +using System; namespace CtrEditor { @@ -602,7 +603,22 @@ namespace CtrEditor var panelEdicion = _mainWindow.PanelEdicion; if (panelEdicion != null && panelEdicion.IsKeyboardFocusWithin) { - UserControlFactory.ForzarActualizacionBindings(panelEdicion.PropertyGrid); + // Solo forzar actualización si NO es el PLCControl el que tiene el foco + var plcControl = _mainWindow.PLCSim; + if (plcControl != null && plcControl.IsKeyboardFocusWithin) + { + System.Diagnostics.Debug.WriteLine("PLCControl tiene foco - NO forzar actualización"); + System.IO.File.AppendAllText("c:\\temp\\debug_focus.txt", + $"{DateTime.Now}: PLCControl tiene foco - NO forzar actualización\n"); + // No forzar actualización si el PLCControl tiene el foco + } + else + { + System.Diagnostics.Debug.WriteLine("PanelEdicion tiene foco - forzar actualización"); + System.IO.File.AppendAllText("c:\\temp\\debug_focus.txt", + $"{DateTime.Now}: PanelEdicion tiene foco - forzar actualización\n"); + UserControlFactory.ForzarActualizacionBindings(panelEdicion.PropertyGrid); + } } } diff --git a/ObjetosSim/UserControlFactory.cs b/ObjetosSim/UserControlFactory.cs index 88d9d6c..271b6e5 100644 --- a/ObjetosSim/UserControlFactory.cs +++ b/ObjetosSim/UserControlFactory.cs @@ -2,13 +2,14 @@ using System.Windows.Controls; using CtrEditor.Simulacion; using System.Reflection; - +using System; using System.Windows.Data; using System.Windows; using System.Windows.Media; using CtrEditor.ObjetosSim.UserControls; using System.Collections; using System.Windows.Input; +using System.Windows.Input; using System.Windows.Shapes; using Xceed.Wpf.Toolkit.PropertyGrid; using System.ComponentModel; @@ -101,7 +102,27 @@ namespace CtrEditor.ObjetosSim { try { - // Método 1: Forzar el foco fuera del PropertyGrid para activar LostFocus + // Verificar si el PropertyGrid pertenece al PLCControl para no interferir con su edición + var plcControl = FindParentOfType(propertyGrid); + if (plcControl != null) + { + // Debug - log que encontramos el PLCControl + System.Diagnostics.Debug.WriteLine("PLCControl detectado - saltando forzado de foco"); + System.IO.File.AppendAllText("c:\\temp\\debug_focus.txt", + $"{DateTime.Now}: PLCControl detectado - saltando forzado de foco\n"); + // Si es del PLCControl, solo forzar actualización de bindings sin robar el foco + var plcBindingExpression = BindingOperations.GetBindingExpression(propertyGrid, PropertyGrid.SelectedObjectProperty); + plcBindingExpression?.UpdateSource(); + ForzarActualizacionControlesEditores(propertyGrid); + return; + } + + // Debug - log que NO es PLCControl + System.Diagnostics.Debug.WriteLine("NO es PLCControl - aplicando forzado de foco normal"); + System.IO.File.AppendAllText("c:\\temp\\debug_focus.txt", + $"{DateTime.Now}: NO es PLCControl - aplicando forzado de foco normal\n"); + + // Método 1: Forzar el foco fuera del PropertyGrid para activar LostFocus (solo para PropertyGrid principal) if (propertyGrid.IsKeyboardFocusWithin) { // Crear un elemento temporal para robar el foco @@ -121,8 +142,8 @@ namespace CtrEditor.ObjetosSim } // Método 2: Forzar la actualización de todos los bindings del PropertyGrid - var bindingExpression = BindingOperations.GetBindingExpression(propertyGrid, PropertyGrid.SelectedObjectProperty); - bindingExpression?.UpdateSource(); + var propertyGridBindingExpression = BindingOperations.GetBindingExpression(propertyGrid, PropertyGrid.SelectedObjectProperty); + propertyGridBindingExpression?.UpdateSource(); // Método 3: Buscar controles de edición activos y forzar su actualización ForzarActualizacionControlesEditores(propertyGrid); @@ -232,6 +253,25 @@ namespace CtrEditor.ObjetosSim propertyGrid.SelectedObject = selectedObject; } + /// + /// Busca un control padre de un tipo específico en el árbol visual + /// + /// El tipo de control padre a buscar + /// El control hijo desde donde empezar la búsqueda + /// El control padre del tipo especificado o null si no se encuentra + private static T FindParentOfType(DependencyObject child) where T : DependencyObject + { + DependencyObject parent = VisualTreeHelper.GetParent(child); + + if (parent == null) + return null; + + if (parent is T parentOfType) + return parentOfType; + + return FindParentOfType(parent); + } + }