Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 文本框自动完成/自动建议_C#_Wpf_Xaml_Mvvm_Autocomplete - Fatal编程技术网

C# 文本框自动完成/自动建议

C# 文本框自动完成/自动建议,c#,wpf,xaml,mvvm,autocomplete,C#,Wpf,Xaml,Mvvm,Autocomplete,是否可以在WPF应用程序的文本框中添加自动完成建议? 比如我在哪里将建议绑定到数据表或字符串列表?使用文本框可以这样做吗 <TextBox Text="{Binding InputText, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"

是否可以在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>

如果您在试图找出从何处开始时遇到困难,实际上有几个步骤,可能有几种方法可以做到这一点

在我的头顶上,你可以创建一个隐藏的
列表框
,它显示在你的
文本框下,包含你的建议(确保
列表框
大小与内容相符)。当文本更改时,您可以使用一个简单的
TestChanged
事件

XAML:

然后,单击
列表框中的一个项目将更新文本。
注意:当用户
列表框中选择建议时,您需要某种方法来防止事件运行逻辑

现在,对于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,您可以相对轻松地绑定
列表框
可见性和内容,然后根据需要显示它

另一种方法是编辑
文本框
默认值,使其具有内置的
列表框
。但这条路要复杂得多


希望这能让您开始。

是否可以在WPF应用程序的文本框中添加自动完成建议
是。你能告诉我们你试过什么吗,上面的代码不能保证你问的是什么。我什么都没试过,只是做了一些研究,但我没有发现关于这个的任何东西。对于一个简单的文本框
,我什么都没试过,请在这里发布之前做。我们帮助特定问题的用户已经尝试过了。现在的问题是寻求更多的“为我编写代码”。我尝试过使用带有自动完成框的外部库,但不想为此购买许可证。你真的发现了吗?
    private void someTextbox_TextChanged(object sender, TextChangedEventArgs e)
    {
        // Call method to check for possible suggestions.
        // Display Listbox with suggested items.
    }
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.
    }
}