From 689c6a9a500ab46ac42b2beddb52c57bf8569a29 Mon Sep 17 00:00:00 2001 From: Curtis Wensley Date: Thu, 17 Sep 2020 09:58:36 -0700 Subject: [PATCH] Wpf: Ensure DataContext is set back when CustomCell is reloaded --- src/Eto.Wpf/Forms/Cells/CustomCellHandler.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Eto.Wpf/Forms/Cells/CustomCellHandler.cs b/src/Eto.Wpf/Forms/Cells/CustomCellHandler.cs index e609b83c1f..35ee237a4f 100755 --- a/src/Eto.Wpf/Forms/Cells/CustomCellHandler.cs +++ b/src/Eto.Wpf/Forms/Cells/CustomCellHandler.cs @@ -75,6 +75,8 @@ public class EtoBorder : swc.Border public Column Column { get; set; } public string Identifier { get; set; } + + public bool NeedsDataContext { get; set; } } public class Column : swc.DataGridColumn @@ -244,6 +246,7 @@ static void HandleControlDataContextChanged(object sender, sw.DependencyProperty wpfctl.Child = child.ToNative(); } handler.Callback.OnConfigureCell(handler.Widget, args, child); + wpfctl.NeedsDataContext = false; handler.FormatCell(wpfctl, cell, wpfctl.DataContext); } @@ -253,10 +256,23 @@ static void HandleControlLoaded(object sender, sw.RoutedEventArgs e) // WPF's loaded event is called more than once, e.g. when on a tab that is not initially visible. var wpfctl = sender as EtoBorder; var etoctl = wpfctl.Control; + var cell = wpfctl?.GetParent(); + var col = cell?.Column as Column; + var handler = col?.Handler; + if (etoctl != null && !etoctl.Loaded) { etoctl.GetWpfFrameworkElement()?.SetScale(true, true); etoctl.AttachNative(); + + // we got loaded, set data context back if needed + if (wpfctl.NeedsDataContext && handler != null) + { + var args = GetEditArgs(handler, cell, wpfctl); + args.SetDataContext(wpfctl.DataContext); + handler.Callback.OnConfigureCell(handler.Widget, args, etoctl); + wpfctl.NeedsDataContext = false; + } } } @@ -279,6 +295,8 @@ static void HandleControlUnloaded(object sender, sw.RoutedEventArgs e) var args = GetEditArgs(handler, cell, wpfctl); args.SetDataContext(null); handler.Callback.OnConfigureCell(handler.Widget, args, etoctl); + + wpfctl.NeedsDataContext = true; } } }