Skip to content

Commit

Permalink
Merge pull request #2228 from cwensley/curtis/wpf-combobox-background…
Browse files Browse the repository at this point in the history
…-color-before-loaded

Wpf: Fix setting TextColor/BackgroundColor of DropDown/ComboBox
  • Loading branch information
cwensley authored May 18, 2022
2 parents 1b4821b + b1d6c26 commit c1dc09e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
13 changes: 13 additions & 0 deletions src/Eto.Wpf/Forms/Controls/ComboBoxHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,19 @@ public override Color BackgroundColor
}
}

public override Color TextColor
{
get => Control.TextBox?.Foreground.ToEtoColor() ?? base.TextColor;
set
{
var textBox = Control.TextBox;
if (textBox != null)
textBox.Foreground = value.ToWpfBrush();
else
PerformOnLoad(() => TextColor = value);
}
}

public bool ReadOnly
{
get { return Control.IsReadOnly; }
Expand Down
10 changes: 5 additions & 5 deletions src/Eto.Wpf/Forms/Controls/DropDownHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,26 +205,26 @@ public override Color BackgroundColor
{
var border = BorderControl;
if (border != null)
{
border.Background = value.ToWpfBrush(border.Background);
}
else
PerformOnLoad(() => BackgroundColor = value);
}
}

public override Color TextColor
{
get
{
var block = Control.FindChild<swc.TextBlock>();
return block != null ? block.Foreground.ToEtoColor() : base.TextColor;
return block?.Foreground.ToEtoColor() ?? base.TextColor;
}
set
{
var block = Control.FindChild<swc.TextBlock>();
if (block != null)
block.Foreground = value.ToWpfBrush();
else
base.TextColor = value;
PerformOnLoad(() => TextColor = value);
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/Eto.Wpf/Forms/WpfFrameworkElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class WpfFrameworkElement
internal const string CustomCursor_DataKey = "Eto.CustomCursor";

internal static IWpfFrameworkElement LastDragTarget;

internal static readonly object LoadActionList_Key = new object();
}

public abstract partial class WpfFrameworkElement<TControl, TWidget, TCallback> : WidgetHandler<TControl, TWidget, TCallback>, Control.IHandler, IWpfFrameworkElement
Expand Down Expand Up @@ -1053,5 +1055,27 @@ public void Print()
}
WpfFrameworkElementHelper.ShouldCaptureMouse = false;
}

internal bool PerformOnLoad(Action action)
{
if (Control.IsLoaded)
return false;
var actionList = Widget.Properties.Get<List<Action>>(WpfFrameworkElement.LoadActionList_Key);
if (actionList == null)
{
actionList = new List<Action>();
Control.Loaded += (sender, e) =>
{
for (int i = 0; i < actionList.Count; i++)
{
actionList[i]();
}
actionList.Clear();
};
Widget.Properties.Set(WpfFrameworkElement.LoadActionList_Key, actionList);
}
actionList.Add(action);
return true;
}
}
}
21 changes: 21 additions & 0 deletions test/Eto.Test/UnitTests/Forms/Controls/ListControlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,26 @@ public void ChangingSelectedIndexMultipleTimesBeforeLoadShouldTriggerChanged()
Assert.AreEqual(2, changed, "3.1 - Setting selected index again should trigger event again");
Assert.AreEqual(1, list.SelectedIndex, "3.2");
}

[Test, ManualTest]
public void ColorsShouldBeSet()
{
ManualForm("Control should have blue background with yellow text", form =>
{
return new TableLayout
{
Rows = {
new TableRow(new T {
BackgroundColor = Colors.Blue,
TextColor = Colors.Yellow,
DataStore = new[] { "Item 1", "Item 2", "Item 3" },
SelectedIndex = 0
}, null),
null
}
};
});
}

}
}

0 comments on commit c1dc09e

Please sign in to comment.