文本框 Autocomple/Autosuggestions
Textbox Autocomple/Autosuggestions
是否可以在 WPF 应用程序中以某种方式向文本框添加自动完成建议?
就像我在哪里将建议绑定到数据表或字符串列表?这可以用文本框实现吗?
<TextBox Text="{Binding InputText, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
VerticalContentAlignment="Center" >
<TextBox.InputBindings>
<KeyBinding Command="{Binding EnterKeyPressedCommand}" Key="Return" />
</TextBox.InputBindings>
</TextBox>
如果您在尝试找出从哪里开始时遇到困难,实际上涉及多个步骤,并且可能有多种方法可以做到这一点。
在我的脑海中,您可以创建一个隐藏的 ListBox
,显示在您的 TextBox
下方,其中包含您的建议(确保 ListBox
大小符合内容)。随着文本的变化,您可以使用一个简单的 TestChanged
事件。
XAML:
<TextBox x:Name="someTextbox"
TextChanged="someTextbox_TextChanged"
</TextBox>
隐藏代码:
private void someTextbox_TextChanged(object sender, TextChangedEventArgs e)
{
// Call method to check for possible suggestions.
// Display Listbox with suggested items.
}
然后,单击 Listbox
中的项目将更新文本。
注意:当用户选择来自[=13=的建议时,您需要一些方法来阻止运行逻辑事件]
现在用于 MVVM:
private string _SomeTextbox = "";
public string SomeTextbox
{
get { return _SomeTextbox; }
set
{
_SomeTextbox = value;
OnPropertyChanged(new PropertyChangedEventArgs("SomeTextbox"));
// Call method to check for possible suggestions.
// Display Listbox with suggested items.
}
}
使用 MVVM,您可以相对轻松地绑定 ListBox
可见性和内容,然后根据需要显示它。
另一种 方法是将 TextBox
默认值编辑为内置 ListBox
。不过这条路要复杂得多。
希望这能让你入门。
是否可以在 WPF 应用程序中以某种方式向文本框添加自动完成建议? 就像我在哪里将建议绑定到数据表或字符串列表?这可以用文本框实现吗?
<TextBox Text="{Binding InputText, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
VerticalContentAlignment="Center" >
<TextBox.InputBindings>
<KeyBinding Command="{Binding EnterKeyPressedCommand}" Key="Return" />
</TextBox.InputBindings>
</TextBox>
如果您在尝试找出从哪里开始时遇到困难,实际上涉及多个步骤,并且可能有多种方法可以做到这一点。
在我的脑海中,您可以创建一个隐藏的 ListBox
,显示在您的 TextBox
下方,其中包含您的建议(确保 ListBox
大小符合内容)。随着文本的变化,您可以使用一个简单的 TestChanged
事件。
XAML:
<TextBox x:Name="someTextbox"
TextChanged="someTextbox_TextChanged"
</TextBox>
隐藏代码:
private void someTextbox_TextChanged(object sender, TextChangedEventArgs e)
{
// Call method to check for possible suggestions.
// Display Listbox with suggested items.
}
然后,单击 Listbox
中的项目将更新文本。
注意:当用户选择来自[=13=的建议时,您需要一些方法来阻止运行逻辑事件]
现在用于 MVVM:
private string _SomeTextbox = "";
public string SomeTextbox
{
get { return _SomeTextbox; }
set
{
_SomeTextbox = value;
OnPropertyChanged(new PropertyChangedEventArgs("SomeTextbox"));
// Call method to check for possible suggestions.
// Display Listbox with suggested items.
}
}
使用 MVVM,您可以相对轻松地绑定 ListBox
可见性和内容,然后根据需要显示它。
另一种 方法是将 TextBox
默认值编辑为内置 ListBox
。不过这条路要复杂得多。
希望这能让你入门。