以编程方式在列表框中选择一个项目 [英] Programmatically selecting an Item in a ListBox

查看:45
本文介绍了以编程方式在列表框中选择一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个 ListBoxes ( CompanyName 代表 QuoteNumber ),并且从WCF中加载数据客户端,请使用以下方法将其添加到 CompanyName 列表框中:

I have three ListBoxes (CompanyName, Representative & QuoteNumber) and I load data from my WCF client, into the CompanyName list box using the method below:

private async Task LoadCompanies()
{
    using (TruckServiceClient client = new TruckServiceClient())
    {
        var companies = await client.GetCompaniesAsync();
        foreach (var company in companies)
            lbxCompanyName.Items.Add(new ListBoxViewItem<Company>(company));
    }
}

现在在下面的代码中,我允许我自己在 lbxCompanyName 列表框中选择公司名称,然后查看属于该公司的代表我的 lbxRepresentative 列表框中的公司.

Now in the coding below I allow myself to select the Company Name in the lbxCompanyName ListBox and then viewing the Representatives that belongs to that Company in my lbxRepresentative ListBox.

private void lbxCompanyName_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var company = (ListBoxViewItem<Company>)lbxCompanyName.SelectedItem; //Changing this line to auto select an Item for me
    foreach (var rep in company.Item.Represetatives)
        lbxRepresentatives.Items.Add(new ListBoxViewItem<Represetative>(rep));
} 

我要实现的是从 CompanyName 列表框中自动/以编程方式选择名称,例如"Josh".我该如何使用现在的编码来做到这一点?

What I want to achieve is to auto/programmatically select the name, let's say "Josh", from the CompanyName ListBox. How would I go about doing this with the coding that I have now?

基本上我想隐藏我的列表框,然后让程序为我选择所有内容.

Basically I want to hide my listboxes and let my program select everything for me.

推荐答案

出于测试目的,我通过将前两个ListBoxes(CompanyName,代表) SelectedIndex 设置为0,然后使用编码解决了问题下面显示我的最后一个ListBox(QuoteNumber),以选择我的QuoteNumber列表框中的最后插入的行.

For testing purposes I solved my issue by setting my first two ListBoxes (CompanyName, Representative)SelectedIndex to 0 and then using the coding below for my last ListBox (QuoteNumber) to select the last inserted row in my QuoteNumber listbox.

    if (lbxQuoteNumber.Items.Count > -1)
        lbxQuoteNumber.SelectedIndex = lbxQuoteNumber.Items.Count - 1;

感谢所有帮助家伙!:)

Thanks for all the help dudes! :)

这篇关于以编程方式在列表框中选择一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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