Implementada la función para forzar la actualización de bindings en el PropertyGrid antes de limpiar la selección y al cambiar el objeto seleccionado. Se mejoró la gestión del foco en el PropertyGrid para asegurar la correcta actualización de los datos.
This commit is contained in:
parent
0d8780b16f
commit
380bc14b69
|
@ -25,6 +25,8 @@ namespace CtrEditor.Controls
|
|||
}
|
||||
|
||||
public bool IsKeyboardFocusWithin => PropertyGridControl.IsKeyboardFocusWithin;
|
||||
|
||||
public PropertyGrid PropertyGrid => PropertyGridControl;
|
||||
|
||||
private void ImagePathButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
|
|
|
@ -484,6 +484,17 @@ namespace CtrEditor
|
|||
|
||||
public void ClearSelection()
|
||||
{
|
||||
// Forzar la actualización de bindings pendientes antes de limpiar la selección
|
||||
if (_mainWindow.DataContext is MainViewModel viewModel && viewModel.SelectedItemOsList != null)
|
||||
{
|
||||
// Obtener el PropertyGrid del PanelEdicion y forzar actualización de bindings
|
||||
var panelEdicion = _mainWindow.PanelEdicion;
|
||||
if (panelEdicion != null && panelEdicion.IsKeyboardFocusWithin)
|
||||
{
|
||||
UserControlFactory.ForzarActualizacionBindings(panelEdicion.PropertyGrid);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var obj in _selectedObjects.ToList())
|
||||
{
|
||||
DeselectObject(obj);
|
||||
|
@ -491,9 +502,9 @@ namespace CtrEditor
|
|||
RemoveAllSelectionHighlights();
|
||||
RemoveResizeRectangles();
|
||||
|
||||
if (_mainWindow.DataContext is MainViewModel viewModel)
|
||||
if (_mainWindow.DataContext is MainViewModel viewModel2)
|
||||
{
|
||||
viewModel.SelectedItemOsList = null;
|
||||
viewModel2.SelectedItemOsList = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -17,6 +17,7 @@ using System.Globalization;
|
|||
using Xceed.Wpf.Toolkit;
|
||||
using Xceed.Wpf.Toolkit.PropertyGrid.Editors;
|
||||
using CtrEditor.ObjetosSim.Extraccion_Datos;
|
||||
using System.Windows.Controls.Primitives;
|
||||
|
||||
namespace CtrEditor.ObjetosSim
|
||||
{
|
||||
|
@ -88,14 +89,90 @@ namespace CtrEditor.ObjetosSim
|
|||
|
||||
public static void LimpiarPropiedadesosDatos(PropertyGrid propertyGrid)
|
||||
{
|
||||
// Forzar la actualización de bindings pendientes antes de limpiar
|
||||
ForzarActualizacionBindings(propertyGrid);
|
||||
|
||||
// Clear previous properties
|
||||
propertyGrid.SelectedObject = null;
|
||||
propertyGrid.PropertyDefinitions.Clear();
|
||||
}
|
||||
|
||||
public static void ForzarActualizacionBindings(PropertyGrid propertyGrid)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Método 1: Forzar el foco fuera del PropertyGrid para activar LostFocus
|
||||
if (propertyGrid.IsKeyboardFocusWithin)
|
||||
{
|
||||
// Crear un elemento temporal para robar el foco
|
||||
var tempButton = new System.Windows.Controls.Button();
|
||||
var parent = propertyGrid.Parent as Panel;
|
||||
if (parent != null)
|
||||
{
|
||||
parent.Children.Add(tempButton);
|
||||
tempButton.Focus();
|
||||
parent.Children.Remove(tempButton);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Alternativa: mover el foco al PropertyGrid mismo
|
||||
propertyGrid.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
|
||||
}
|
||||
}
|
||||
|
||||
// Método 2: Forzar la actualización de todos los bindings del PropertyGrid
|
||||
var bindingExpression = BindingOperations.GetBindingExpression(propertyGrid, PropertyGrid.SelectedObjectProperty);
|
||||
bindingExpression?.UpdateSource();
|
||||
|
||||
// Método 3: Buscar controles de edición activos y forzar su actualización
|
||||
ForzarActualizacionControlesEditores(propertyGrid);
|
||||
|
||||
// Pequeña pausa para permitir que se procesen los eventos
|
||||
System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke(
|
||||
System.Windows.Threading.DispatcherPriority.Background,
|
||||
new Action(() => { }));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Log del error si es necesario, pero no interrumpir el flujo
|
||||
System.Diagnostics.Debug.WriteLine($"Error al forzar actualización de bindings: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private static void ForzarActualizacionControlesEditores(DependencyObject parent)
|
||||
{
|
||||
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
|
||||
{
|
||||
var child = VisualTreeHelper.GetChild(parent, i);
|
||||
|
||||
// Buscar TextBox, ComboBox, y otros controles de edición comunes
|
||||
if (child is TextBox textBox)
|
||||
{
|
||||
var expression = textBox.GetBindingExpression(TextBox.TextProperty);
|
||||
expression?.UpdateSource();
|
||||
}
|
||||
else if (child is ComboBox comboBox)
|
||||
{
|
||||
var expression = comboBox.GetBindingExpression(ComboBox.SelectedValueProperty);
|
||||
expression?.UpdateSource();
|
||||
var textExpression = comboBox.GetBindingExpression(ComboBox.TextProperty);
|
||||
textExpression?.UpdateSource();
|
||||
}
|
||||
else if (child is CheckBox checkBox)
|
||||
{
|
||||
var expression = checkBox.GetBindingExpression(CheckBox.IsCheckedProperty);
|
||||
expression?.UpdateSource();
|
||||
}
|
||||
|
||||
// Recursión para buscar en controles hijos
|
||||
ForzarActualizacionControlesEditores(child);
|
||||
}
|
||||
}
|
||||
|
||||
public static void CargarPropiedadesosDatos(osBase selectedObject, PropertyGrid propertyGrid)
|
||||
{
|
||||
// Forzar la actualización de bindings pendientes antes de cambiar el objeto
|
||||
ForzarActualizacionBindings(propertyGrid);
|
||||
|
||||
// Limpia las propiedades previas
|
||||
propertyGrid.SelectedObject = null;
|
||||
|
|
Loading…
Reference in New Issue