gpt4 book ai didi

c# - 在控制焦点上的 WPF 中打开 AutoCompleteBox

转载 作者:行者123 更新时间:2023-11-30 14:31:29 28 4
gpt4 key购买 nike

我试图在控件焦点上打开 System.Windows.Controls.AutoCompleteBox。事件触发但没有任何反应:/当我开始输入一些文本时,自动完成框工作正常。我究竟做错了什么?

AutoCompleteBox box = new AutoCompleteBox();
box.Text = textField.Value ?? "";
box.ItemsSource = textField.Proposals;
box.FilterMode = AutoCompleteFilterMode.Contains;
box.GotFocus += (sender, args) =>
{
box.IsDropDownOpen = true;
};

最佳答案

我做了一个快速的解决方法,好像这个解决方案在我的程序中让我满意。

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;
}
};
}

关于c# - 在控制焦点上的 WPF 中打开 AutoCompleteBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20121184/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com