从WPF中的列表框中获取复选框项

从WPF中的列表框中获取复选框项

本文介绍了从WPF中的列表框中获取复选框项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发WPF应用程序.我在其中以以下方式将CheckBoxes添加到ListBox中.

I am developing WPF application. In which I am adding CheckBoxes to a ListBox in following way.

foreach (User ls in lst)
{
     AddContacts(ls, lstContactList);
}

private void AddContacts(User UserData, ListBox lstbox)
{
    try
    {
        var txtMsgConversation = new CheckBox()
        {

                Padding = new Thickness(1),
                IsEnabled = true,
                //IsReadOnly = true,
                Background = Brushes.Transparent,
                Foreground = Brushes.White,
                Width = 180,
                Height = 30,
                VerticalAlignment = VerticalAlignment.Top,
                VerticalContentAlignment = VerticalAlignment.Top,
                Content = UserData.Name, //+ "\n" + UserData.ContactNo,
                Margin = new Thickness(10, 10, 10, 10)
        };

        var SpConversation = new StackPanel() { Orientation = Orientation.Horizontal };

        SpConversation.Children.Add(txtMsgConversation);

        var item = new ListBoxItem()
        {
                Content = SpConversation,
                Uid = UserData.Id.ToString(CultureInfo.InvariantCulture),
                Background = Brushes.Black,
                Foreground = Brushes.White,
                BorderThickness = new Thickness(1),
                BorderBrush = Brushes.Gray
        };


        item.Tag = UserData;

        lstbox.Items.Add(item);
    }
    catch (Exception ex)
    {
        //Need to log Exception
    }
}

现在,我需要从ListBox获取已选中的项目.我在这里如何进行操作,我尝试了下面的代码,该代码返回null,

Now I need to get the checked items from ListBox. How do I proceed here, I tried below code, which returning null,

CheckBox chkBox = lstContactList.SelectedItem as CheckBox;

任何建议

问候Sangeetha

RegardsSangeetha

推荐答案

在列表框中创建动态多个项目的方法不在代码隐藏中,而是为这些项目创建模板,然后将其绑定到项目列表.

The way of creating dynamic multiple items in a listbox is not in codebehind, but to create a template for the items, and then bind it to a list of items.

示例

说我有一堆段落List<Passage> Passages { get; set; }:

public class Passage
{
  public string Name { get; set; }
  public bool IsSelected { get; set; }
}

在我的xaml中,我创建一个模板并将其绑定

In my xaml I create a template and bind to it

<ListBox ItemsSource="{Binding Passages}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel  Orientation="Horizontal">
                <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}" />
                <TextBlock Text="{Binding Path=Name, StringFormat=Passage: {0}}"
                           Foreground="Blue" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

对于我的四个段落"Alpha","Beta","Gamma"和"I-25",结果看起来像这样:

The result looks like this for my four passages "Alpha", "Beta", "Gamma" and "I-25":

然后,如果我想要选定的项目,例如上面最近选中的Beta,我只列举选定项目的列表.

Then if I want the selected item, such as the recently checked Beta above, I just enumerate my List for the selected one(s).

 var selecteds = Passages.Where(ps => ps.IsSelected == true);


是否需要在一个ListBox中列出不同类型的对象?说从绑定到复合集合还是ObservableCollection<T>?

在这里查看我的答案:

  • Composite Collection
  • ObservableCollection

这篇关于从WPF中的列表框中获取复选框项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 02:27