0
我一直在試圖重現這個例子:http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx樹視圖不顯示數據
出於某種原因,我的TreeView的是完全無法顯示任何數據,我想不通爲什麼(我沒有得到任何錯誤,不要」不知道如何正確調試)。這是我的代碼:
MainWindow.XAML
<Window x:Class="Tryout.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Tryout.domain"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<TreeView Name="treeViewFiles" HorizontalAlignment="Left" Margin="10,10,0,12" Width="200" ClipToBounds="True">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:Directory}" ItemsSource="{Binding Children}">
<Label Content="{Binding Path=Name}"/>
</HierarchicalDataTemplate>
<DataTemplate DataType ="{x:Type local:File}">
<TextBlock Text="{Binding Path=Name}" />
</DataTemplate>
</TreeView.Resources>
</TreeView>
MainWindow.XAML.cs
namespace Tryout
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Directory rootDirectory = new Directory("Root");
rootDirectory.Children.Add(new Directory("Subdirectory 1"));
rootDirectory.Children.Add(new Directory("Subdirectory 2"));
((Directory)rootDirectory.Children[1]).Children.Add(new File("The only file"));
treeViewFiles.ItemsSource = rootDirectory.Children;
}
}
}
File.cs
namespace Tryout.domain
{
public class File : INotifyPropertyChanged
{
public string Name;
public File(String _name)
{
Name = _name;
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
}
Directory.cs
namespace Tryout.domain
{
public class Directory : File
{
public List<File> Children = new List<File>();
public Directory(String _name) : base(_name) { }
}
}
是的,這解決了我的問題。不知道領域和屬性之間實際上是有區別的。想想我應該閱讀更多關於C#的內容。 – ChristopherS 2011-12-18 12:37:59