我正在使用 .NET Framework 4.0 构建我的应用程序。
我有一个组合框,我想在其中关闭组合框的建议附加模式。相反,我想要仅建议模式。
在许多用户要求关闭自动完成功能的问题中,我得到的都是相同的答案。即,将 IsTextSearchEnabled 设置为 False。
当 IsTextSearchEnabled = True 时
当 IsTextSearchEnabled = False 时
我想要的是:
当用户在组合框上按 Enter 时,我希望将项目附加到组合框的文本框。
这东西在WPF中可行吗?
最佳答案
就像这里 promise 的那样是演示。如您所见,我做了我在评论中解释的事情。我听了文本更改事件。
检查一下:
<Grid>
<local:MyComboBox x:Name="comboBox" IsEditable="True"
VerticalAlignment="Center"
IsTextSearchEnabled="True">
<ComboBoxItem>hello</ComboBoxItem>
<ComboBoxItem>world</ComboBoxItem>
<ComboBoxItem>123</ComboBoxItem>
</local:MyComboBox>
</Grid>
public class MyComboBox : ComboBox
{
private string myValue;
private bool needsUpdate;
public override void OnApplyTemplate()
{
TextBox tbx = this.GetTemplateChild("PART_EditableTextBox") as TextBox;
tbx.PreviewKeyDown += (o, e) =>
{
this.needsUpdate = true;
};
tbx.TextChanged += (o, e) =>
{
if (needsUpdate)
{
myValue = tbx.Text;
this.needsUpdate = false;
}
else
{
tbx.Text = myValue;
}
};
base.OnApplyTemplate();
}
}
关于wpf - 在 wpf 的 Combobox 中关闭自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20291213/