亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

WPF 建議文本框

WPF 建議文本框

C#
幕布斯6054654 2021-11-14 17:30:04
我構建了一個小的 WPF TextBox 來檢查它的內容是否有效?,F在我想實現提供建議的可能性。但不像互聯網上的示例那樣彈出一個帶有建議的列表。我正在尋找一個示例,它可以像這樣選擇 TextBox:如果有我可以查找的特定名稱或您知道的任何示例代碼,請告訴我。
查看完整描述

2 回答

?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

經過與 WPF 的大量斗爭,我有一個為您工作的概念證明:


主窗口.xaml


<Window x:Class="Solutions.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800">

    <Grid>

        <TextBox VerticalAlignment="Center" HorizontalAlignment="Center" x:Name="SuggestionBox" Width="200"

                 />

    </Grid>

</Window>

主窗口.xaml.cs:


using System.Linq;

using System.Windows;

using System.Windows.Controls;


namespace Solutions

{

    public partial class MainWindow : Window

    {

        private static readonly string[] SuggestionValues = {

            "England",

            "USA",

            "France",

            "Estonia"

        };


        public MainWindow()

        {

            InitializeComponent();

            SuggestionBox.TextChanged += SuggestionBoxOnTextChanged;

        }


        private string _currentInput = "";

        private string _currentSuggestion = "";

        private string _currentText = "";


        private int _selectionStart;

        private int _selectionLength;

        private void SuggestionBoxOnTextChanged(object sender, TextChangedEventArgs e)

        {

            var input = SuggestionBox.Text;

            if (input.Length > _currentInput.Length && input != _currentSuggestion)

            {

                _currentSuggestion = SuggestionValues.FirstOrDefault(x => x.StartsWith(input));

                if (_currentSuggestion != null)

                {

                    _currentText = _currentSuggestion;

                    _selectionStart = input.Length;

                    _selectionLength = _currentSuggestion.Length - input.Length;


                    SuggestionBox.Text = _currentText;

                    SuggestionBox.Select(_selectionStart, _selectionLength);

                }

            }

            _currentInput = input;

        }

    }

}

下一步是將其轉換為用戶控件,以便您可以通過綁定設置您的建議,但您可以自行處理。


查看完整回答
反對 回復 2021-11-14
?
HUX布斯

TA貢獻1876條經驗 獲得超6個贊

您可以通過稍微修改此處的解決方案來使用水?。ㄓ绕涫莾H xaml 的答案非常簡短)。只需將水印文本設置為您的第一個建議即可。您可以添加一些功能,例如在代碼隱藏中按退格鍵時刪除建議。


查看完整回答
反對 回復 2021-11-14
  • 2 回答
  • 0 關注
  • 285 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號