【问题标题】:Getting the content of a checkbox within and Items Collection within a ListBox with wpf C#使用wpf C#获取ListBox内的复选框和项目集合的内容
【发布时间】:2015-12-02 19:50:52
【问题描述】:

我是 C# 的新手,我已经有一段时间遇到这个问题了。我有一个名为“listBox”的列表框,它绑定到一个项目集合,该集合有超过 100 个复选框,每个复选框都有不同的内容(或复选框标签)。

我正在尝试遍历每个复选框,如果选中,则将内容打印到控制台中。我能够检测是否检查了某些内容,但绝对不是最有效的方法。请参阅下面的代码:

private void SomeButton_Click(object sender, RoutedEventArgs e)
    {
        string checkState = "";
        for (int i = 0; i < listBox.Items.Count - 1; i++)
        {
            checkState = listBox.Items[i].ToString().Substring(listBox.Items[i].ToString().Length - 5, 5);
            if (checkState != "False")
            {
                Console.WriteLine("Item " + i + " is checked");
            }
        }
    }

此代码确实适用于检测是否检查了某些内容。但是,如果我能够从 ItemsCollection 中的复选框中获取实际的真/假属性,效率会更高。我已经尝试了多种方法来尝试获取选中状态以及复选框内容,但遗憾的是,我每次尝试都变得枯燥无味。以下是我尝试从这些复选框之一获取内容的一些内容:

Console.WriteLine(listBox.Items[i].ToString());

Console.WriteLine(listBox.Items.GetItemAt(i).ToString());

Console.WriteLine(listBox.Items.GetItemAt(i).Equals(0).ToString());

任何帮助将不胜感激。

【问题讨论】:

  • 感谢@CoderForHire 的快速响应。这看起来可以让我获得选中状态,但不能让我获得复选框内容。此外。我将 tat question 的答案复制到我的列表框中,但我收到了一个错误的标记错误。 &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;CheckBox Content="{Binding .}" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=IsSelected" /&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt;

标签: c# wpf xaml checkbox listbox


【解决方案1】:

理想情况下,您希望在您的集合中拥有一类数据对象,而不是 UI 对象的集合。

public class MyDataObject : INotifyPropertyChanged
{
    public string Caption { get; set; }
    public bool IsChecked { get; set; }
}

var items = new ObservableCollection<MyDataObject>();
// TODO: populate collection

listBox.ItemsSource = items;

现在当你绑定它时,listbox.Items 包含你的ObservableCollection&lt;MyDataObject&gt;,你可以从那里检查值

private void SomeButton_Click(object sender, RoutedEventArgs e)
{
    foreach(MyDataObject item in listBox.Items)
    {
        if (item.IsChecked)
            Console.WriteLine("Item " + item.Caption + " is checked");
    }
}

附带说明,如果您不需要选择行为,ItemsControl 可能比 ListBox 更适合一系列控件。 XAML 可能看起来像这样:

<ItemsControl x:Name="myItemsControl">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding Caption}" Checked="{Binding IsChecked}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
var items = new ObservableCollection<MyDataObject>();
items.Add(new MyDataObject() { Caption = "A" });
items.Add(new MyDataObject() { Caption = "B" });
items.Add(new MyDataObject() { Caption = "C" });
items.Add(new MyDataObject() { Caption = "D" });
items.Add(new MyDataObject() { Caption = "E" });

myItemsControl.ItemsSource = items;

【讨论】:

  • 谢谢,稍后我会调查一下!可能不是今天,如果我不能立即回复您,请见谅!
  • 没问题,如果您需要我澄清任何事情,请告诉我:)
  • 我还是 C# 新手,在这里遇到了一些问题!我试着用谷歌搜索,但我想不出任何有用的东西。它似乎向我的 ItemCollection 添加了一个项目,我需要使用 Items.Add("foo"); 但是,我不确定在哪里添加此代码(我再次为对这种语言的无知道歉)。这是属于 MyDataObject 类还是任何类?
  • 我有最后一期,我几乎已经弄清楚了。我通过以下方式将项目添加到项目集合中:CheckBox Itm0 = new CheckBox(); Itm0.Content = new TextBlock { Text = "Example Text" + "/n", TextWrapping = TextWrapping.WrapWithOverflow }; items.Add(Itm0); items.Add(Itm0); 行导致错误,指出我无法将 Systems.Windows.Controls.Checkboc 转换为 MyDataObject。感谢您迄今为止的所有帮助!
  • @AlexWeber 我在答案中添加了一些示例 XAML。您根本不应该为集合创建 UI 对象。只需将集合绑定到具有正确模板的 ItemsControl 或 ListBox,它就会自动为您生成 UI 项。
猜你喜欢
  • 2019-10-27
  • 1970-01-01
  • 2018-02-05
  • 1970-01-01
  • 1970-01-01
  • 2013-09-03
  • 1970-01-01
  • 2011-02-21
  • 2011-02-01
相关资源
最近更新 更多