Skip to content

Commit

Permalink
Merge pull request picoe#887 from picoe/fix/wpf-customcell-loading-ex…
Browse files Browse the repository at this point in the history
…ception

Fix issue with WPF when using a custom cell on a tab.
  • Loading branch information
cwensley authored Sep 19, 2017
2 parents 7dd4115 + 6f123d0 commit cb76a89
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
1 change: 1 addition & 0 deletions Source/Eto.Test/Eto.Test/Eto.Test - pcl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
<Compile Include="UnitTests\Drawing\GraphicsTests.cs" />
<Compile Include="UnitTests\Drawing\MatrixTests.cs" />
<Compile Include="UnitTests\Forms\Controls\ComboBoxTests.cs" />
<Compile Include="UnitTests\Forms\Controls\CustomCellTests.cs" />
<Compile Include="UnitTests\Forms\Controls\NumericStepperTests.cs" />
<Compile Include="UnitTests\Forms\Controls\PanelTests.cs" />
<Compile Include="UnitTests\Forms\Controls\SplitterTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using Eto.Forms;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Eto.Test.UnitTests.Forms.Controls
{
[TestFixture]
public class CustomCellTests : TestBase
{
class MyModel
{
public string Text { get; set; }
}

class MyCustomCell : StackLayout
{
public MyCustomCell()
{
Orientation = Orientation.Horizontal;
Items.Add(new Label { Text = "Hello!" });
var tb = new TextBox();
tb.TextBinding.BindDataContext((MyModel m) => m.Text);
Items.Add(new StackLayoutItem(tb, true));
}
}

/// <summary>
/// Test for WPF crash when a grid view and custom cell is on a separate tab and you switch to it.
/// </summary>
[Test]
public void CustomCellOnSeparateTabLoadIssue()
{
TabControl tabs = null;
Shown(form =>
{
var grid1 = new GridView { ShowHeader = false };
grid1.Columns.Add(new GridColumn { DataCell = CustomCell.Create<MyCustomCell>() });
grid1.DataStore = new List<MyModel>
{
new MyModel { Text = "Item 1"},
new MyModel { Text = "Item 2"},
new MyModel { Text = "Item 3"},
};

var grid2 = new GridView { ShowHeader = false };
grid2.Columns.Add(new GridColumn { DataCell = CustomCell.Create<MyCustomCell>() });
grid2.DataStore = new List<MyModel>
{
new MyModel { Text = "Item 1"},
new MyModel { Text = "Item 2"},
new MyModel { Text = "Item 3"},
};

tabs = new TabControl();
tabs.Pages.Add(new TabPage(grid1) { Text = "Tab 1" });
tabs.Pages.Add(new TabPage(grid2) { Text = "Tab 2" });
form.Content = tabs;
}, () =>
{
tabs.SelectedIndex = 1;
});
}
}
}
7 changes: 5 additions & 2 deletions Source/Eto.Wpf/Forms/Cells/CustomCellHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,15 @@ EtoBorder Create(swc.DataGridCell cell)
control.Unloaded += (sender, e) =>
{
var ctl = sender as EtoBorder;
ctl.Control?.DetachNative();
if (ctl.Control.Loaded)
ctl.Control?.DetachNative();
};
control.Loaded += (sender, e) =>
{
// WPF's loaded event is called more than once, e.g. when on a tab that is not initially visible.
var ctl = sender as EtoBorder;
ctl.Control?.AttachNative();
if (!ctl.Control.Loaded)
ctl.Control?.AttachNative();
};
control.DataContextChanged += (sender, e) =>
{
Expand Down

0 comments on commit cb76a89

Please sign in to comment.