Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wpf: Fix DropDown.SelectedIndexChanged before control is loaded #1972

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 3 additions & 44 deletions src/Eto.Wpf/Forms/Controls/DropDownHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

namespace Eto.Wpf.Forms.Controls
{

public class DropDownHandler : DropDownHandler<EtoComboBox, DropDown, DropDown.ICallback>
{
internal static readonly object AllowVirtualization_Key = new object();
Expand All @@ -23,36 +24,12 @@ public class DropDownHandler : DropDownHandler<EtoComboBox, DropDown, DropDown.I

public class EtoComboBox : swc.ComboBox, IEtoWpfControl
{
int? selected;

public EtoComboBox()
{
Loaded += ComboBoxEx_Loaded;
}

public bool IsVirtualizing
{
get => swc.VirtualizingPanel.GetIsVirtualizing(this);
set => swc.VirtualizingPanel.SetIsVirtualizing(this, value);
}

public override void OnApplyTemplate()
{
base.OnApplyTemplate();

if (!IsLoaded)
{
var lastSelected = SelectedIndex;
if (lastSelected != -1)
{
selected = lastSelected;
SelectedIndex = -1;
}
}
}

internal void SetSelected(int? index) => selected = index;

public swc.Primitives.Popup Popup => GetTemplateChild("PART_Popup") as swc.Primitives.Popup;

public swc.ScrollViewer ContentHost
Expand All @@ -70,12 +47,6 @@ public swc.ScrollViewer ContentHost

public IWpfFrameworkElement Handler { get; set; }

protected override void OnSelectionChanged(swc.SelectionChangedEventArgs e)
{
if (selected == null)
base.OnSelectionChanged(e);
}

protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
base.OnItemsChanged(e);
Expand All @@ -85,13 +56,6 @@ protected override void OnItemsChanged(System.Collections.Specialized.NotifyColl
}
}

void ComboBoxEx_Loaded(object sender, sw.RoutedEventArgs e)
{
if (selected == null) return;
SelectedIndex = selected.Value;
selected = null;
}

sw.Size FindMaxSize(sw.Size constraint)
{
var size = base.MeasureOverride(constraint);
Expand Down Expand Up @@ -225,13 +189,8 @@ private void SetVirtualization()

public int SelectedIndex
{
get { return Control.SelectedIndex; }
set
{
Control.SelectedIndex = value;
if (!Control.IsLoaded)
Control.SetSelected(value);
}
get => Control.SelectedIndex;
set => Control.SelectedIndex = value;
}

protected virtual swc.Border BorderControl => Control.FindChild<swc.Border>();
Expand Down
41 changes: 41 additions & 0 deletions test/Eto.Test.Wpf/UnitTests/DropDownTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Eto.Forms;
using Eto.Test.UnitTests;
using NUnit.Framework;

namespace Eto.Test.Wpf.UnitTests
{
[TestFixture]
public class DropDownTests : TestBase
{
[Test, InvokeOnUI]
public void DropDownInElementHostShouldHaveCorrectInitialValue()
{
var dropDown = new DropDown();
dropDown.DataStore = new [] { "Item 1", "Item 2", "Item 3" };

var content = TableLayout.AutoSized(dropDown);

// forces template to be applied to WPF controls
var native = content.ToNative(false);
native.Measure(new System.Windows.Size(double.PositiveInfinity, double.PositiveInfinity));

// now set selected index now that template has applied
dropDown.SelectedIndex = 1;

// check that the index is correct when it is shown
int? dropDownIndex = null;

var dlg = new Dialog();
dlg.ClientSize = new Drawing.Size(200, 200);
dlg.Content = content;
dlg.Shown += (sender, e) => {
dropDownIndex = dropDown.SelectedIndex;
dlg.Close();
};
dlg.ShowModal(Application.Instance.MainForm);

Assert.AreEqual(1, dropDownIndex);
}

}
}
20 changes: 20 additions & 0 deletions test/Eto.Test/UnitTests/Forms/Controls/ListControlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,25 @@ public void SettingDataStoreToNullAfterPopulatedShouldNotCrash()
};
});
}

[Test, InvokeOnUI]
public void ChangingSelectedIndexMultipleTimesBeforeLoadShouldTriggerChanged()
{
var list = new T();
int changed = 0;
list.SelectedIndexChanged += (sender, e) => changed++;
list.DataStore = new [] { "Item 1", "Item 2", "Item 3" };

Assert.AreEqual(0, changed, "1.1 - Setting data store should not fire selected index");
Assert.AreEqual(-1, list.SelectedIndex, "1.2");

list.SelectedIndex = 0;
Assert.AreEqual(1, changed, "2.1 - Setting selected index should trigger event");
Assert.AreEqual(0, list.SelectedIndex, "2.2");

list.SelectedIndex = 1;
Assert.AreEqual(2, changed, "3.1 - Setting selected index again should trigger event again");
Assert.AreEqual(1, list.SelectedIndex, "3.2");
}
}
}