WPF智能输入提示

6 篇文章 0 订阅
1 篇文章 0 订阅

WPF智能输入提示是用一个textBox和Popup完成的,下面看具体代码:

<Window x:Class="WPFPopup.ABCD"

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

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

Title="ABCD" Height="300" Width="300">

<Grid>

<TextBox Canvas.Left="371" Canvas.Top="25" Height="23" Name="textBox1" Width="120" KeyUp="textBox1_KeyUp" Margin="75,79,83,159" />

<TextBlock Height="23" HorizontalAlignment="Left" Margin="75,44,0,0" Name="textBlock1" Text="TextBlock" VerticalAlignment="Top" Width="155" />

</Grid>

</Window>


 

namespace WPFPopup

{

/// <summary>

/// ABCD.xaml 的交互逻辑

/// </summary>

public partial class ABCD : Window

{

public List<string> listStr;

public ABCD()

{

InitializeComponent();

listStr = new List<string>();

listStr.Add("a");

listStr.Add("ab");

listStr.Add("abc");

listStr.Add("abcd");

listStr.Add("b");

listStr.Add("bc");

listStr.Add("bcd");

listStr.Add("c");

listStr.Add("cd");

listStr.Add("d");

listStr.Add("da");

listStr.Add("dab");

listStr.Add("dabc");

}

public Popup pop = new Popup();

private void textBox1_KeyUp(object sender, KeyEventArgs e)

{

if (textBox1.Text != "")

{

var item = listStr.Where(a => a.Contains(textBox1.Text));

if (item.ToList<string>().Count > 0)

{

pop = this.createPopup(pop, item.ToList<string>(), textBox1);

pop.IsOpen = true;

}

else

{

pop.IsOpen = false;

}

}

else

{

pop.IsOpen = false;

}





}

public void listbox_MouseDoubleClick(object sender, MouseButtonEventArgs e)

{

ListBox box = sender as ListBox;

string itemvalue = box.SelectedValue as string;

this.textBlock1.Text = itemvalue;

this.textBox1.Text = itemvalue;

pop.IsOpen = false;

}

public Popup createPopup(Popup pop, List<string> listSource, UIElement element)

{

Border border = new Border();

border.BorderBrush = new SolidColorBrush(Colors.Black);

//border.BorderThickness = new Thickness(1.0);//设置边框宽度



StackPanel panel1 = new StackPanel();

panel1.Children.Clear();

panel1.Background = new SolidColorBrush(Colors.LightGray);



ListBox listbox = new ListBox();

listbox.Background = new SolidColorBrush(Colors.WhiteSmoke);

listbox.MinWidth = 100;

listbox.Height = 120;

listbox.ItemsSource = listSource;

listbox.MouseDoubleClick += new MouseButtonEventHandler(listbox_MouseDoubleClick);



panel1.Children.Add(listbox);

border.Child = panel1;



pop.Child = border;

pop.PlacementTarget = element;

return pop;

}

}

}


 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值