WPF以编程方式在列表框中选择多个项目 [英] WPF select multiple items in a listbox programmatically

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

问题描述

我试图找出在从数据库中检索数据后如何选择列表框中的某些项目,但我不知道如何。 以下是我到目前为止:



 lb_temp.DisplayMemberPath = "关键英寸; 
lb_temp.SelectedValuePath =" Value" ;;

lb_temp.Items.Add(new KeyValuePair< string,string>(" YES"," Y"));
lb_temp.Items.Add(new KeyValuePair< string,string>(" NO"," N")); 

for(int i = 0; i< lb_temp.Items.Count; i ++)
{  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;&NBSP;&NBSP;
  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;
ListBoxItem lbi =(ListBoxItem)lb_temp.ItemContainerGenerator.ContainerFromIndex(i);  &NBSP; &NBSP;
  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;
if(lbi.Content.ToString()==" N")
lbi.IsSelected = true;  &NBSP; &NBSP;
  &NBSP; &NBSP; &NBSP; &NBSP;
}



我遇到的问题是lbi.Content.ToString()返回值{[NO,N]}。 我不确定如何获得'N'。



解决方案

你好wegoodwin,


根据你的描述,你在ListBox中添加字典,然后你想从ListBoxItem获取字典值,我做一个你的样本可以看看。

< ListBox Name =" listbox" ItemsSource =" {Binding dic}"的SelectionChanged = QUOT; listbox_SelectionChanged"> 
< ListBox.ItemTemplate>
< DataTemplate>
< StackPanel Orientation =" Horizo​​ntal">
< TextBlock Width =" 50" Text =" {Binding Key}" />
< TextBlock Text =" {Binding Value}" />
< / StackPanel>
< / DataTemplate>
< /ListBox.ItemTemplate>
< / ListBox>


 public partial class Window18:Window 
{
public Dictionary< string,string> dic {get;组; }
public Window18()
{
InitializeComponent();
dic = new Dictionary< string,string>();
dic.Add(" Yes"," Y");
dic.Add("No","N");

this.DataContext = this;
}

private void listbox_SelectionChanged(object sender,SelectionChangedEventArgs e)
{
var selectedItem =(KeyValuePair< string,string>)listbox.SelectedItem;
string item = selectedItem.Value;
Console.WriteLine(item);

}
}


最好的问候,


Cherry



I am trying to figure out how select certain items in a listbox after I retrieve data from a database, but I am at a loss as to how.  Here is what I have so far:

lb_temp.DisplayMemberPath = "Key";
lb_temp.SelectedValuePath = "Value";

lb_temp.Items.Add(new KeyValuePair<string, string>("YES", "Y"));
lb_temp.Items.Add(new KeyValuePair<string, string>("NO", "N")); 

for (int i = 0; i < lb_temp.Items.Count; i++)
{              
                  
ListBoxItem lbi = (ListBoxItem)lb_temp.ItemContainerGenerator.ContainerFromIndex(i);     
                   
if (lbi.Content.ToString() == "N")
lbi.IsSelected = true;     
           
}

The problem that I have is that lbi.Content.ToString() returns the value {[NO,N]}.  I am not sure how to just get the 'N'.

解决方案

Hi wegoodwin,

According to your description, you add dictionary in ListBox, then you want to get Dictionary value from ListBoxItem, I do one sample that you can take a look.

<ListBox Name="listbox" ItemsSource="{Binding dic}" SelectionChanged="listbox_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Width="50" Text="{Binding Key}" />
                        <TextBlock Text="{Binding Value}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

public partial class Window18 : Window
    {
        public Dictionary<string,string> dic { get; set; }
        public Window18()
        {
            InitializeComponent();
            dic = new Dictionary<string, string>();
            dic.Add("Yes","Y");
            dic.Add("No", "N");

            this.DataContext = this;
        }

        private void listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedItem = (KeyValuePair<string, string>)listbox.SelectedItem;
            string item = selectedItem.Value;
            Console.WriteLine(item);
            
        }
    }

Best Regards,

Cherry


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

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