如何从TreeView中获取未选中的复选框 [英] how to get unchecked checkboxes from treeview
问题描述
大家好.
如何使用C#从Treeview控件中获取未经检查的节点?
如果有人知道请让我知道.
我的文件夹结构是这样的:
-a
-b
| _c
| _d
| _e
| | _f
| | _g
| _h
| _i
| _j
| _k
| _l
所有具有复选框的节点,但我未选中b folder.w文件夹是否位于b下,这些复选框也未选中.现在我想要未选中的复选框的文本.
hi to all..
how to get unchecked nodes from treeview control using c#?
if any body knows pls let me know.
my folder structure like this:
-a
-b
|_c
|_d
|_e
| |_f
| |_g
|_h
|_i
|_j
|_k
|_l
all nodes having checkboxes but i unchecked b folder.wat ever the folders are there in under b those checkboxes also unchecked. now i want the text for unchecked checkboxes.
how?
推荐答案
如果您使用的是WPF,则可能会遇到如下问题:
if you are working with WPF you may have a some thing like this:
public class Check
{
public Check Parent { get; set; }
public string Name { get; set; }
public bool? IsChecked { get; set; }
public List<Check> Child { get; set; }
}
您将拥有:
and you will have this:
private List<Check> _items = new List<Check>();
public List<Check> Items { get { return _items; } set { _items = value; } }
并在构造函数中:
and in the constructor:
DataContext = this;
//To Add The Parents
for (int i = 0; i < 10; i++)
Items.Add(new Check { Name = "Item Number " + i.ToString(), IsChecked = i % 2 == 0 });
//To Add Child For Each Parent
foreach (var item in Items)
item.Child = new List<Check> { new Check { Name = "Child of " + item.Name, IsChecked = !item.IsChecked, Parent = item } };
您将拥有如下方法:
and you will have method like this:
public List<Check> GetUncheckedItems(List<Check> childs)
{
List<Check> uncheckedItems=new List<Check>();
foreach (var item in childs)
{
if (item.IsChecked==null || (bool)!item.IsChecked)
uncheckedItems.Add(item);
if (item.Child != null)
uncheckedItems.AddRange(GetUncheckedItems(item.Child));
}
return uncheckedItems;
}
以及当按钮(单击获取未选中的项目")时:
and when the button (Get Unchecked Items clicked):
private void Button_Click(object sender, RoutedEventArgs e)
{
var result = "";
foreach (var item in GetUncheckedItems(Items))
result += "\n" + item.Name;
MessageBox.Show(result);
}
最终在XAML中您将拥有:
finally in the XAML you will have:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="100*" />
<RowDefinition Height="10*" />
</Grid.RowDefinitions>
<TreeView ItemsSource="{Binding Items}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Child}">
<CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
<Button Content="Get Unchecked" Grid.Row="1" Click="Button_Click" />
</Grid>
这篇关于如何从TreeView中获取未选中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!