2015-07-02 91 views
0

我想创建TreeView与复选框,图像和一些其他信息从UI隐藏。 XAML代码:WPF TreeView绑定不添加元素

<TreeView Grid.Column="0" ItemsSource="{Binding T1}"> 
      <TreeView.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal"> 
         <CheckBox Name="CheckBoxZ" Content="{Binding name}" IsChecked="{Binding box}" Foreground="{Binding color}" Unchecked="CheckBoxZ_Updated" Checked="CheckBoxZ_Updated"/> 
         <Image Source="{Binding image}"/> 
        </StackPanel> 
       </DataTemplate> 
      </TreeView.ItemTemplate> 
     </TreeView> 

C#代码:

namespace App 
{ 
public partial class MainWindow : Window 
{ 
    public class TS : TreeViewItem 
     { 
      public string color { get; set; } 
      public string name { get; set; } 
      public bool box { get; set; } 
      public string path { get; set; } 
      public string source { get; set; } 
      public int operation { get; set; } 
      public ImageSource image { get; set; } 
      public ObservableCollection<TS> Items { get; set; } 
      public TS() 
      { 
       this.Items = new ObservableCollection<TS>(); 
      } 
     } 
    public TS T1, T2; 
    public MainWindow() 
     { 
      InitializeComponent(); 
      T1 = new TS() { color = "Green", name = "folder", box = true, path = "C:\\folder", source = "D:\\folder", operation = 0, image = X.ToImageSource(System.Drawing.Icon.ExtractAssociatedIcon("D:\\folder\\file.txt")) }; 
      T2 = new TS() { color = "Green", name = "file.txt", box = true, path = "C:\\folder\\file.txt", source = "D:\\folder\\file.txt", operation = 2, image = X.ToImageSource(System.Drawing.Icon.ExtractAssociatedIcon("D:\\folder\\file.txt")) }; 
      T1.Items.Add(T2); 
      MessageBoxResult result = System.Windows.MessageBox.Show(T1.Items.Count.ToString() + " " + T2.Items.Count.ToString(), "Title", MessageBoxButton.OK, MessageBoxImage.None); 
     } 
    public static ImageSource ToImageSource(Icon icon) 
     { 
      ImageSource imageSource = Imaging.CreateBitmapSourceFromHIcon(
       icon.Handle, 
       Int32Rect.Empty, 
       BitmapSizeOptions.FromEmptyOptions()); 

      return imageSource; 
     } 
} 

项目被成功添加到变量,但不显示。应用程序开始没有错误。我没有这个故障的变种。出了什么问题?

回答

0

有什么错在这里的几件事情:

  1. WPF只能绑定到性能 - 它不会绑定到你的T1
  2. 您正试图将TreeView.ItemsSource绑定到T1。这需要IEnumerable。您应该绑定类似于ObservableCollection<TS>
  3. 您正在使用DataTemplate。由于每个项目本身可能有子女,因此您应该使用HierarchicalDataTemplate并将其ItemsSource绑定到属性TS

顺便说一下,我看不到为什么TS应该继承TreeViewItem

我会建议遵循其中的许多教程之一,这将引导你通过这样做,如this one

+0

感谢响应。 1:我在复选框的列表框中使用了相同的算法,它工作。我刚刚尝试这个,问题没有解决。我会尝试。 –

+0

*对不起,列表框使用ObservableCollection 。 –

0

ItemsSource应绑定一个ObservableCollection或一些ItemsCollection(Items在你的情况)。

我不明白你是如何设计你的课程的。粗糙的模式应该如下:

public class TS: TreeViewItem{} 
public class TSCollection: ObservableCollection<TS> {} 

和你MainWindow应该是这样的:

public MainWindow() 
     { 
TS T1 = new TS(){Name="folder", Color="Green"}; 
TS T2= new TS(){Name="folder1", Color="Blue"}; 
TSColletion collection = new TSCollection(); 
collection.Add(T1,T2); 
} 

你的XAML应该再有collection作为ItemsSource }

+0

以及如何添加子项? –

+0

我知道你需要ObservableCollection在你的TreeViewItem中有孩子,但我指出它'ItemsSource'应该总是绑定到一个Items集合而不是一个Item – Marshal