2013-07-08 104 views
0

我有从.txt文件中提取后列出的结果列表。我想在列出的每个结果后添加一个复选框。以下是我的代码:将复选框添加到动态生成结果的列表中

private void LoadFile() { 
     List<string> lines = new List<string>(); 
     try 
     { 
      StreamReader sr = new StreamReader("test.txt"); 
      while (!sr.EndOfStream) 
      { 
       lines.Add(sr.ReadLine()); 
      } 
      sr.Close(); 

      for (int i = 3; i < lines.Count; i++) 
      { 
       resultsTreeView.Items.Add(lines[i].ToString().Substring(67,17)); 
       resultsTreeView.Items.Add(CheckBox[i]); 
      } 

如何添加复选框,因为提取的结果每次都会更改?我想跟踪哪些方框已经过检查,以便我可以将结果打印给用户。谢谢!

+0

如果你请你的问题更清晰,并提供在您确定一个复选框有标准待检查与否:) –

+0

嗨@SirajMansour,复选框不需要检查。这取决于用户选择,用户将在他认为必要的复选框上打勾。 –

回答

0
  for (int i = 3; i < lines.Count; i++) 
      { 
       resultsTreeView.Items.Add(lines[i].ToString().Substring(67,17)); 
       resultsTreeView.Items.Add(new CheckBox()); 
       // resultsTreeView.Items.Add(BuildCheckBox()) 
      } 

OR

CheckBox BuildCheckbox() 
{ 
    CheckBox C = new CheckBox(); 
    return C; 
} 

这就是你需要创建一个复选框,也可以创建里面创建复选框的新实例,并设置属性返回一个复选框功能,/订阅按照您想要的方式进行活动并将其退回。

至于该复选框被选中的跟踪,我只需要你提供我与您“resultsTreeView”的类型

编辑:

要遍历TreeView的复选框,并做的事已检查的部分:

resultsTreeView.Items.OfType<CheckBox>().ToList() 
       .ForEach(C => 
       { 
        if (C.IsChecked.HasValue && C.IsChecked.Value == true) 
        { 
         //DoSomething 
        } 
       }); 
+0

非常感谢。 resultsTreeView的类型是否意味着字符串?如果是的话,你可以指导我通过?谢谢! –

+0

对不起,我应该注意到你正在使用WPF。 –

+0

请检查我的编辑,我为你添加循环复选框的方式 –

0

我不确定您到底在找什么。我假设resultsTreeView是一个TreeViewItem。我也假设你在wpf工作。您可以通过wpf执行以下操作。类似

for (int i = 0; i < lines.Count(); i++) 
{ 
    resultsTreeView.Items.Add(lines[i].ToString().Substring(67,17)); 
} 


<TreeView x:Name="resultsTreeView" HorizontalAlignment="Left" Height="100" Margin="37,344,0,0" VerticalAlignment="Top" Width="257" > 
    <TreeView.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Text="{Binding}"/> 
       <CheckBox/> 
      </StackPanel> 
     </DataTemplate> 
    </TreeView.ItemTemplate> 
</TreeView> 

东西可以通过代码来完成后面

for (int i = 0; i < mylist.Count(); i++) 
{ 
    resultsTreeView.Items.Add(mylist[i]); 
} 
resultsTreeView.ItemTemplate = TreeViewDataTemp; 

然后创建TreeViewDataTemp通过以下方式将上述

private static DataTemplate TreeViewDataTemp 
    { 
     get 
     { 
      DataTemplate TVDT = new DataTemplate(); 

      FrameworkElementFactory Stack = new FrameworkElementFactory(typeof(StackPanel)); 
      Stack.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal); 

      FrameworkElementFactory TextB = new FrameworkElementFactory(typeof(TextBlock)); 
      TextB.SetValue(TextBlock.TextProperty, new Binding()); 

      FrameworkElementFactory ChkBox = new FrameworkElementFactory(typeof(CheckBox)); 

      Stack.AppendChild(TextB); 
      Stack.AppendChild(ChkBox); 

      TVDT.VisualTree = Stack; 

      return TVDT; 
     } 
    } 

为您提供了1个项目是文本与复选框一起。 或者你的方法将每个字符串项,您添加后添加一个复选框作为一个新的项目..这是

for (int I=0; I<lines.Count(); I++) 
{ 
    resultsTreeView.Items.Add(mylist[i]); 
    resultsTreeView.Items.Add(new CheckBox()); 
}