【问题标题】:Getting checkbox checked items content in a string array in C#在C#中的字符串数组中获取复选框选中的项目内容
【发布时间】:2012-07-02 06:22:46
【问题描述】:

我有一个包含复选框的列表框。我想在字符串数组中获取所有复选框选中的项目内容。我怎样才能得到这个?

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
    <ListBox x:Name="TrackingView1" Margin="9,0,2,5"> 
        <ListBox.ItemTemplate> 
            <DataTemplate> 
                <CheckBox x:Name="fileNameLinkButton" Content="{Binding BindsDirectlyToSource=True}" FontFamily="Segoe WP Semibold" Foreground="Black" FontSize="20" /> 
            </DataTemplate> 
        </ListBox.ItemTemplate> 
    </ListBox> 
</Grid>

【问题讨论】:

  • 请提供您的代码 (XAML) 以及到目前为止您尝试了什么?
  • @NikhilAgrawal ` `
  • @NikhilAgrawal ListBox ItemSource 绑定到一个字符串数组,其中包含来自隔离存储的文件名。
  • 您将 ListBox 绑定到什么?它应该绑定到包含表示 CheckBox 状态的 bool 元素的 ObservableCollection。如果您有这种情况,您可以只查询列表中已检查为 true 的元素。
  • @MBen 我的 ListBox 绑定到一个字符串[],其中包含隔离存储中的文件名。

标签: c# windows-phone-7


【解决方案1】:

您可以使用 VisualTreeHelper 检索数据模板项, 使用该方法获取datatemplate的第一项

 //method for finding first element of the listbox data template
    private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
    {
        var count = VisualTreeHelper.GetChildrenCount(parentElement);
        if (count == 0)
            return null;
        for (int i = 0; i < count; i++)
        {
            var child = VisualTreeHelper.GetChild(parentElement, i);
            if (child != null && child is T)
            { return (T)child; }
            else
            {
                var result = FindFirstElementInVisualTree<T>(child);
                if (result != null)
                    return result;
            }
        }
        return null;
    }

然后在您想要的情况下使用此代码,例如单击按钮

 int itemsCount = this.TrackingView1.Items.Count;
 List<string> myList = new  List<string>();
 for (int i = 0; i < itemsCount; i++)
 {
            ListBoxItem item = (ListBoxItem)this.TrackingView1.ItemContainerGenerator.ContainerFromIndex(i);
            CheckBox tagregCheckBox = FindFirstElementInVisualTree<CheckBox>(item);
            if((bool)tagregCheckBox.IsChecked)                
              myList.Add(tagregCheckBox.Content.ToString());
 }

【讨论】:

  • 完美答案!这正是我想要的。
  • 一切正常,但是当我在“var count = VisualTreeHelper.GetChildrenCount(parentElement);”行调试“引用不是有效的视觉依赖对象”异常时我首先为一些复选框项目工作,但一段时间后我得到这个异常并且我的应用程序关闭。当遇到异常时,parentElement 等于 null。你有什么想法吗?
  • 似乎有更多项目出现此错误。我还没有找到解决方案,但我会尽快在此处发布。如果您也知道如何解决它,请也写在这里。谢谢
【解决方案2】:

在经典的 TextBook 方法中,您可以将“TrackingView1”绑定到 IList,其中 PersonClass 类似于

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

    public PersonClass(string name, bool isSelected)
    {
    this.Name = name;
    this.IsSelected = isSelected;
    }
}

在您想要收集数据的地方,

string[] checkedNames = (from name in Names
            where name.IsSelected
            select name.Name).ToArray();

我实际上会避免使用 ToArray(),并直接访问 UI

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-28
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    • 2017-06-09
    • 1970-01-01
    • 2016-11-20
    • 1970-01-01
    相关资源
    最近更新 更多