Mejorar la gestión del foco en la interfaz, evitando interferencias con el PLCControl. Se implementaron verificaciones para forzar actualizaciones de bindings sin robar el foco y se añadió un método para buscar controles padres en el árbol visual.
This commit is contained in:
parent
3b953b7998
commit
cad650b3d0
|
@ -387,8 +387,8 @@ namespace CtrEditor
|
||||||
|
|
||||||
private void MainWindow_KeyDown(object sender, KeyEventArgs e)
|
private void MainWindow_KeyDown(object sender, KeyEventArgs e)
|
||||||
{
|
{
|
||||||
// Only force canvas focus if PanelEdicion doesn't have focus
|
// Only force canvas focus if PanelEdicion doesn't have focus AND PLCControl doesn't have focus
|
||||||
if (!PanelEdicion.IsKeyboardFocusWithin)
|
if (!PanelEdicion.IsKeyboardFocusWithin && !PLCSim.IsKeyboardFocusWithin)
|
||||||
{
|
{
|
||||||
if (!ImagenEnTrabajoCanvas.IsFocused)
|
if (!ImagenEnTrabajoCanvas.IsFocused)
|
||||||
{
|
{
|
||||||
|
@ -819,8 +819,8 @@ namespace CtrEditor
|
||||||
|
|
||||||
private void Canvas_KeyDown(object sender, KeyEventArgs e)
|
private void Canvas_KeyDown(object sender, KeyEventArgs e)
|
||||||
{
|
{
|
||||||
// Only handle if PanelEdicion doesn't have focus
|
// Only handle if PanelEdicion doesn't have focus AND PLCControl doesn't have focus
|
||||||
if (!PanelEdicion.IsKeyboardFocusWithin)
|
if (!PanelEdicion.IsKeyboardFocusWithin && !PLCSim.IsKeyboardFocusWithin)
|
||||||
{
|
{
|
||||||
HandleKeyDown(e);
|
HandleKeyDown(e);
|
||||||
}
|
}
|
||||||
|
@ -828,8 +828,8 @@ namespace CtrEditor
|
||||||
|
|
||||||
private void ScrollViewer_PreviewKeyDown(object sender, KeyEventArgs e)
|
private void ScrollViewer_PreviewKeyDown(object sender, KeyEventArgs e)
|
||||||
{
|
{
|
||||||
// Only handle if PanelEdicion doesn't have focus
|
// Only handle if PanelEdicion doesn't have focus AND PLCControl doesn't have focus
|
||||||
if (!PanelEdicion.IsKeyboardFocusWithin &&
|
if (!PanelEdicion.IsKeyboardFocusWithin && !PLCSim.IsKeyboardFocusWithin &&
|
||||||
(e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Up || e.Key == Key.Down))
|
(e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Up || e.Key == Key.Down))
|
||||||
{
|
{
|
||||||
HandleKeyDown(e);
|
HandleKeyDown(e);
|
||||||
|
|
|
@ -7,6 +7,7 @@ using System.Windows.Input;
|
||||||
using System.Windows.Media;
|
using System.Windows.Media;
|
||||||
using System.Windows.Shapes;
|
using System.Windows.Shapes;
|
||||||
using Color = System.Windows.Media.Color;
|
using Color = System.Windows.Media.Color;
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace CtrEditor
|
namespace CtrEditor
|
||||||
{
|
{
|
||||||
|
@ -602,7 +603,22 @@ namespace CtrEditor
|
||||||
var panelEdicion = _mainWindow.PanelEdicion;
|
var panelEdicion = _mainWindow.PanelEdicion;
|
||||||
if (panelEdicion != null && panelEdicion.IsKeyboardFocusWithin)
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,13 +2,14 @@
|
||||||
using System.Windows.Controls;
|
using System.Windows.Controls;
|
||||||
using CtrEditor.Simulacion;
|
using CtrEditor.Simulacion;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System;
|
||||||
using System.Windows.Data;
|
using System.Windows.Data;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Media;
|
using System.Windows.Media;
|
||||||
using CtrEditor.ObjetosSim.UserControls;
|
using CtrEditor.ObjetosSim.UserControls;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Windows.Input;
|
using System.Windows.Input;
|
||||||
|
using System.Windows.Input;
|
||||||
using System.Windows.Shapes;
|
using System.Windows.Shapes;
|
||||||
using Xceed.Wpf.Toolkit.PropertyGrid;
|
using Xceed.Wpf.Toolkit.PropertyGrid;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
|
@ -101,7 +102,27 @@ namespace CtrEditor.ObjetosSim
|
||||||
{
|
{
|
||||||
try
|
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<LibS7Adv.PLCControl>(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)
|
if (propertyGrid.IsKeyboardFocusWithin)
|
||||||
{
|
{
|
||||||
// Crear un elemento temporal para robar el foco
|
// 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
|
// Método 2: Forzar la actualización de todos los bindings del PropertyGrid
|
||||||
var bindingExpression = BindingOperations.GetBindingExpression(propertyGrid, PropertyGrid.SelectedObjectProperty);
|
var propertyGridBindingExpression = BindingOperations.GetBindingExpression(propertyGrid, PropertyGrid.SelectedObjectProperty);
|
||||||
bindingExpression?.UpdateSource();
|
propertyGridBindingExpression?.UpdateSource();
|
||||||
|
|
||||||
// Método 3: Buscar controles de edición activos y forzar su actualización
|
// Método 3: Buscar controles de edición activos y forzar su actualización
|
||||||
ForzarActualizacionControlesEditores(propertyGrid);
|
ForzarActualizacionControlesEditores(propertyGrid);
|
||||||
|
@ -232,6 +253,25 @@ namespace CtrEditor.ObjetosSim
|
||||||
propertyGrid.SelectedObject = selectedObject;
|
propertyGrid.SelectedObject = selectedObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Busca un control padre de un tipo específico en el árbol visual
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">El tipo de control padre a buscar</typeparam>
|
||||||
|
/// <param name="child">El control hijo desde donde empezar la búsqueda</param>
|
||||||
|
/// <returns>El control padre del tipo especificado o null si no se encuentra</returns>
|
||||||
|
private static T FindParentOfType<T>(DependencyObject child) where T : DependencyObject
|
||||||
|
{
|
||||||
|
DependencyObject parent = VisualTreeHelper.GetParent(child);
|
||||||
|
|
||||||
|
if (parent == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
if (parent is T parentOfType)
|
||||||
|
return parentOfType;
|
||||||
|
|
||||||
|
return FindParentOfType<T>(parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue