纸上得来终觉浅,绝知此事要躬行。

 

WPF智能提示示例<一>

对于这样一个小示例。不再做什么讲解,主要是实现思路清晰就够了。先看一下结果:

核心代码[XAML]:

<Page x:Class="AutoCompleteWPF.Pages.Home"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" Loaded="_Home_OnLoaded" KeyUp="_Home_OnKeyUp"
      d:DesignHeight="300" d:DesignWidth="300"
    Title="Home">

    <Grid Height="300" Width="300">
        <Grid.RowDefinitions>
            <RowDefinition Height="30"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>

        <TextBox x:Name="boxAutoComplete" Width="300" Height="27" Padding="0,2,0,0" Grid.Row="0" TextChanged="_BoxAutoComplete_OnTextChanged"></TextBox>
        <ListBox x:Name="lboxSource" Grid.Row="1" Width="300" SelectionChanged="_LboxSource_OnSelectionChanged"></ListBox>
    </Grid>
</Page>

 核心代码[C#]:

// ===============================================================================
//  Copyright © jRoger.NET. All Rights Reserved .
//  个人网站:http://jRoger.net/.
//  创建时间:2013-04-07 22:17
//  版权所有:jRoger.
// ===============================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace AutoCompleteWPF.Pages
{
    /// <summary>
    /// Home.xaml 的交互逻辑
    /// </summary>
    public partial class Home
    {
        private List<string> _source; 
        public Home() { this.InitializeComponent(); }

        private void _Home_OnLoaded(object sender, RoutedEventArgs e)
        {
            _source = new List<string>{
                "jRoger",
                "Home",
                "nokia",
                "tom",
                "jacko",
                "htc",
                "研究生",
                "本科",
                "本来不在乎"
            };
        }

        private void _BoxAutoComplete_OnTextChanged(object sender, TextChangedEventArgs e)
        {
            var box = sender as TextBox;
            
            if (box == null || string.IsNullOrWhiteSpace(box.Text))
            {
                this.lboxSource.ItemsSource = null;
                this.lboxSource.SelectedIndex = -1;
                this._index = -1;

                return;
            }

            var source = _source.Where(u => u.IndexOf(box.Text, StringComparison.InvariantCultureIgnoreCase) != -1).ToList();

            this.lboxSource.Visibility = Visibility.Visible;
            this.lboxSource.ItemsSource = source.Any() ? source : null;
            this.lboxSource.SelectedIndex = -1;
            this._index = -1;
        }

        private int _index = -1;
        private bool _keyIn;

        private void _Home_OnKeyUp(object sender, KeyEventArgs e)
        {
            this._keyIn = true;

            if (this.lboxSource.ItemsSource != null && e.Key == Key.Up)
            {
                if (this._index > 0)
                {
                    this._index--;

                    this.lboxSource.SelectedIndex = this._index;
                }
                else if (this._index == 0)
                {
                    this._index = this.lboxSource.Items.Count - 1;

                    this.lboxSource.SelectedIndex = this._index;
                }
                else if (this._index == (this.lboxSource.Items.Count - 1))
                {
                    this._index = 0;

                    this.lboxSource.SelectedIndex = this._index;
                }
            }

            if (this.lboxSource.ItemsSource != null && e.Key == Key.Down)
            {

                if (this._index >= -1 && this._index != (this.lboxSource.Items.Count - 1))
                {
                    this._index++;

                    this.lboxSource.SelectedIndex = this._index;
                }
                else if (this._index == (this.lboxSource.Items.Count - 1))
                {
                    this._index = 0;

                    this.lboxSource.SelectedIndex = this._index;
                }
            }

            if (this.lboxSource.ItemsSource == null || e.Key != Key.Enter) return;

            var text = this.lboxSource.SelectedItem.ToString();

            this.boxAutoComplete.Text = text;
            this.boxAutoComplete.Focus();
            this.boxAutoComplete.SelectionStart = text.Length;
            this.lboxSource.Visibility = Visibility.Hidden;

            this._keyIn = false;
        }

        private void _LboxSource_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this._keyIn) return;

            var text = this.lboxSource.SelectedItem.ToString();

            this.boxAutoComplete.Text = text;

            this.lboxSource.Visibility = Visibility.Hidden;
        }
    }
}

 就这么点儿代码。明天如果有时间封装成一个通用WPF控件。

posted on 2013-04-07 23:58  JRoger  阅读(785)  评论(0编辑  收藏  举报

导航