WPF-搜索带有按钮的列表框? [英] WPF - Search ListBox with a Button?

查看:95
本文介绍了WPF-搜索带有按钮的列表框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我还有一个问题,希望我还没弄复杂.

简要项目说明:
我正在尝试制作一个由ListBox(通过Linq从SQL获取数据)和26 Buttons(每个字母一个)组成的触摸屏应用程序.每个ListBox项中显示三个字段,即名称",地址"和电话号码".

问题:
是否可以通过按按钮"的特定字母开头的名称"转到列表框中的字段?

我想保持所有字段可见,并跳转到以"S"开头的名称(例如).

更多信息:
我一直在寻找,并且TextSearch类不断出现,但是我不确定如何将其应用于我的情况.

我当时想我可以使用下面的代码来消除每个按钮都需要自己的代码的需求.

Hi Guys, I have another question, I hope I haven''t over complicated it.

Brief Project Description:
I am trying to make a touchscreen application that consists of a ListBox (getting data from SQL via Linq) and 26 Buttons (one for each letter of the alphabet). There are three fields displayed in each ListBox item, "Name", "Address" and "Phone Number".

Question:
Is it possible to goto fields in the ListBox with a "Name" starting with the particular letter of the Button pressed?

I would like to keep all fields visible and jump to Names starting with "S" (for example).

More Information:
I have been looking around and TextSearch class keeps coming up but i am not really sure how to apply it to my situation.

I was thinking i could use the below code to eliminate the need for each button needing its own code.

private void SearchLetterButton(object sender, RoutedEventArgs args)
{
    Button btn = sender as Button;
    string buttondata = btn.Content;
    /// Listbox Search Code using "buttondata" as the search criteria
}


预先感谢您的任何帮助,
亚历克斯.


Thanks any help in advance,
Alex.

推荐答案

大家好,抱歉延迟回覆你们两个,

谢谢克里斯蒂安,我看了看ScrollViewer控件,它似乎只允许您自动滚动到第一和最后一个项目.有ScrollViewer.ScrollToHorizo​​ntalOffset,但是您需要知道要滚动到的项目的SelectedIndex,并且知道SelectedIndex只是问题的一半.

我对TextSearch类进行了更多研究,这似乎很有希望.如果启用了TextSearch并将TextPath设置在要搜索的字段上,则当您按键盘上的字母时,它将选择列表中具有该字母的第一项.现在所需要做的就是用按钮单击代替键盘输入.

TextSearch示例代码:
Hi Guys, Sorry for the delay in getting back to you both,

Thanks Christian, I had a look at the ScrollViewer Control and it seems to only allow you to scroll to the first and last items automatically. There is ScrollViewer.ScrollToHorizontalOffset but you need to know the SelectedIndex of the item that you want to scroll to and knowing the SelectedIndex is half the problem.

I looked more into the TextSearch class and it seems a little promising. If you enable TextSearch and set the TextPath on the field you want to search, when you press a letter on the keyboard it selects the first item in the list with that letter. All that is needed now is to substitute the keyboard input with a button click.

TextSearch Example Code:
<Window.Resource>
    <DataTemplate x:Key="listboxTemplate">
        <TextBlock Text="{Binding Path=Name}"/>
    </DataTemplate>
</Window.Resources>

<Grid>
    <ListBox x:Name="listbox" ItemTemplate="{StaticResource listboxTemplate}"
    IsTextSearchEnabled="True" TextSearch.TextPath="Name"/>
</Grid>

var query = from s in dc.People
            orderby s.Name
            select s;
listbox.ItemsSource = query.ToList();



我知道这种方法很粗糙,但是我发现了一些虚拟键盘模拟示例,这可能是达到目的的一种方法.下面的代码适用于TextBox,但我还没有弄清楚如何使其适合于ListBox.



I know its kind of crude but i have found a couple of samples of virtual keyboard simulation that might be a means to an end. The code below works for TextBox''s but i haven''t quite worked out how to adapt it to work with a ListBox.

void keyboardButton_Click(object sender, RoutedEventArgs e)
{
    if (System.Windows.Input.Keyboard.FocusedElement.GetType() == typeof(TextBox))
    {
        TextBox box = ((TextBox)System.Windows.Input.Keyboard.FocusedElement);
        int newIndex = box.CaretIndex + 1;
        box.Text = box.Text.Insert(box.CaretIndex, ((Button)sender).Content.ToString());
        box.CaretIndex = newIndex;
    }
}



再次感谢您的答复,请您告诉我是否取得了进展,
亚历克斯



Thanks again for the replies, Ill yet you know if i make any progress,
Alex


[
This[^] looks like a good place to start. Just remember the item that is the first item with each letter, so you can scroll to it.


Dude甚至我都想找出来.... :-D每当我完成我肯定会告诉你...:laugh:
Dude even i m trying to find it out.... :-D Whenever i finished i will definitely tell u... :laugh:


这篇关于WPF-搜索带有按钮的列表框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆