Open AutoCompleteBox in WPF on control focus
autocomplete, c#, wpf
Solution
I did a quick workaround as if this solution is satisfying for me in my program.
AutoCompleteBox box = new AutoCompleteBox();
box.Text = textField.Value ?? "";
if (textField.Proposals != null)
{
box.ItemsSource = textField.Proposals;
box.FilterMode = AutoCompleteFilterMode.None;
box.GotFocus += (sender, args) =>
{
if (string.IsNullOrEmpty(box.Text))
{
box.Text = " "; // when empty, we put a space in the box to make the dropdown appear
}
box.Dispatcher.BeginInvoke(() => box.IsDropDownOpen = true);
};
box.LostFocus += (sender, args) =>
{
box.Text = box.Text.Trim();
};
box.TextChanged += (sender, args) =>
{
if (!string.IsNullOrWhiteSpace(box.Text) &&
box.FilterMode != AutoCompleteFilterMode.Contains)
{
box.FilterMode = AutoCompleteFilterMode.Contains;
}
if (string.IsNullOrWhiteSpace(box.Text) &&
box.FilterMode != AutoCompleteFilterMode.None)
{
box.FilterMode = AutoCompleteFilterMode.None;
}
};
}
Problem
I'm trying to open `System.Windows.Controls.AutoCompleteBox` on control focus. The event triggers but nothing happens:/ When I start entering some text, the autocomplete box works fine. What am I doing wrong? ``` AutoCompleteBox box = new AutoCompleteBox(); box.Text = textField.Value ?? ""; box.ItemsSource = textField.Proposals; box.FilterMode = AutoCompleteFilterMode.Contains; box.GotFocus += (sender, args) => { box.IsDropDownOpen = true; }; ```