Skip to content

Commit

Permalink
Wpf: Fix memory leaks when using a CustomCell
Browse files Browse the repository at this point in the history
Also fix leak when using a long lived inner binding for an ObjectBinding
  • Loading branch information
cwensley committed Jul 23, 2020
1 parent d76e5c3 commit 43fc38e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
35 changes: 21 additions & 14 deletions src/Eto.Wpf/Forms/Cells/CustomCellHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ EtoBorder Create(swc.DataGridCell cell)
return control;
}

private void HandlePreviewMouseDown(object sender, MouseButtonEventArgs e)
static void HandlePreviewMouseDown(object sender, MouseButtonEventArgs e)
{
var ctl = sender as sw.FrameworkElement;
var cell = ctl?.GetVisualParent<swc.DataGridCell>();
Expand All @@ -152,24 +152,28 @@ private void HandlePreviewMouseDown(object sender, MouseButtonEventArgs e)
}
}

void HandleIsKeyboardFocusWithinChanged(object sender, DependencyPropertyChangedEventArgs e)
static void HandleIsKeyboardFocusWithinChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var h = Handler;
var ctl = sender as sw.FrameworkElement;
var cell = ctl?.GetVisualParent<swc.DataGridCell>();
var ctl = sender as EtoBorder;
var cell = ctl?.GetParent<swc.DataGridCell>();
var col = cell?.Column as Column;
var handler = col?.Handler;
if (handler == null)
return;

var isEditing = ctl.IsKeyboardFocusWithin || cell.IsEditing;
var args = GetEditArgs(cell, ctl);
var args = GetEditArgs(handler, cell, ctl);
if (args?.IsEditing != isEditing)
{
args.SetIsEditing(isEditing);
//cell.IsEditing = isEditing;
if (isEditing)
{
h.Callback.OnBeginEdit(h.Widget, args);
handler.Callback.OnBeginEdit(handler.Widget, args);
}
else {
h.Callback.OnCommitEdit(h.Widget, args);
h.ContainerHandler.CellEdited(h, ctl);
handler.Callback.OnCommitEdit(handler.Widget, args);
handler.ContainerHandler.CellEdited(handler, ctl);
}
}
}
Expand Down Expand Up @@ -276,7 +280,11 @@ static void HandleControlUnloaded(object sender, sw.RoutedEventArgs e)
var wpfctl = sender as EtoBorder;
var ctl = wpfctl.Control;
if (ctl != null && ctl.Loaded)
{
// clear out the context to remove any bindings
wpfctl.DataContext = null;
ctl.DetachNative();
}
}

static void HandleCellSelectedChanged(object sender, sw.RoutedEventArgs e)
Expand Down Expand Up @@ -311,7 +319,7 @@ protected override object PrepareCellForEdit(sw.FrameworkElement editingElement,
var obj = base.PrepareCellForEdit(editingElement, editingEventArgs);
var handler = Handler;
var cell = editingElement?.GetParent<swc.DataGridCell>();
var args = GetEditArgs(cell, editingElement);
var args = GetEditArgs(handler, cell, editingElement);
if (handler != null && args != null)
{
args.SetIsEditing(true);
Expand All @@ -326,9 +334,8 @@ protected override object PrepareCellForEdit(sw.FrameworkElement editingElement,
return obj;
}

WpfCellEventArgs GetEditArgs(swc.DataGridCell cell, FrameworkElement editingElement)
static WpfCellEventArgs GetEditArgs(CustomCellHandler handler, swc.DataGridCell cell, FrameworkElement editingElement)
{
var handler = Handler;
if (handler == null)
return null;
var ctl = editingElement as EtoBorder;
Expand All @@ -347,7 +354,7 @@ protected override bool CommitCellEdit(sw.FrameworkElement editingElement)
var result = base.CommitCellEdit(editingElement);
var handler = Handler;
var cell = editingElement?.GetParent<swc.DataGridCell>();
var args = GetEditArgs(cell, editingElement);
var args = GetEditArgs(handler, cell, editingElement);
if (handler != null && args != null)
{
args.SetIsEditing(false);
Expand All @@ -369,7 +376,7 @@ protected override void CancelCellEdit(sw.FrameworkElement editingElement, objec
{
var handler = Handler;
var cell = editingElement?.GetParent<swc.DataGridCell>();
var args = GetEditArgs(cell, editingElement);
var args = GetEditArgs(handler, cell, editingElement);
if (handler != null && args != null)
{
args.SetIsEditing(false);
Expand Down
17 changes: 5 additions & 12 deletions src/Eto/Forms/Binding/ObjectBinding.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,6 @@ public ObjectBinding(T dataItem, IndirectBinding<TValue> innerBinding)
{
this.dataItem = dataItem;
InnerBinding = innerBinding;
InnerBinding.Changed += HandleInnerBindingChanged;
InnerBinding.Changing += HandleInnerBindingChanging;
}

void HandleInnerBindingChanging(object sender, BindingChangingEventArgs e)
{
OnChanging(e);
}

void HandleInnerBindingChanged(object sender, BindingChangedEventArgs e)
{
OnChanged(e);
}

/// <summary>
Expand All @@ -173,7 +161,12 @@ public override TValue DataValue
}
set
{
var args = new BindingChangingEventArgs(DataValue);
OnChanging(args);
if (args.Cancel)
return;
InnerBinding.SetValue(DataItem, Equals(value, default(T)) ? SettingNullValue : value);
OnChanged(new BindingChangedEventArgs(DataValue));
}
}

Expand Down

0 comments on commit 43fc38e

Please sign in to comment.