从UserControl访问类方法(XAML,C#,TreeView)

时间:2017-05-16 09:44:36

标签: c# wpf xaml namespaces treeview

我尝试在TreeViewModel.TreeViewItem_Expanded中添加一个方法:

public void TreeViewItem_Expanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = e.Source as TreeViewItem;
            if ((item.Items.Count == 1) && (item.Items[0] is string))
            {
                item.Items.Clear();

                DirectoryInfo expandedDir = null;
                if (item.Tag is DriveInfo)
                    expandedDir = (item.Tag as DriveInfo).RootDirectory;
                if (item.Tag is DirectoryInfo)
                    expandedDir = (item.Tag as DirectoryInfo);
                try
                {
                    foreach (DirectoryInfo subDir in expandedDir.GetDirectories())
                        item.Items.Add(CreateTreeItem(subDir));
                }
                catch { }
            }
        }

将TreeView转换为xaml:

 <Grid Background="MidnightBlue">
    <TreeView Name="trvStructure" TreeViewItem.Expanded="TreeViewModel.TreeViewItem_Expanded" Margin="20" />
 </Grid>

我还尝试添加命名空间:

 xmlns:Treemodels="clr-namespace:LayoutMVVM.ViewModels"

然后:

<TreeView Name="trvStructure" TreeViewItem.Expanded="Treemodels.TreeViewModel.TreeViewItem_Expanded" Margin="20" />

但也没有奏效。错误:

enter image description here

1 个答案:

答案 0 :(得分:2)

答案已更新:

好的,这是完整的答案:

XAML:

<TreeView Name="trvStructure" Margin="20" ItemsSource="{Binding Items}">
        <TreeView.Resources>
            <Style TargetType="TreeViewItem">
                <Setter Property="Header" Value="{Binding Name}"/>
                <Setter Property="ItemsSource" Value="{Binding Items}"/>
                <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
            </Style>
        </TreeView.Resources>
</TreeView>

视图模型:

public class MainViewModel
{
    public ObservableCollection<TreeViewItemViewModel> Items { get; } = new ObservableCollection<TreeViewItemViewModel>();
    public MainViewModel()
    {
        foreach (var driveInfo in DriveInfo.GetDrives())
        {
            Items.Add(new DriveTreeViewItemViewModel(driveInfo));
        }
    }
}

public abstract class TreeViewItemViewModel
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    private ICommand _expandedCommand;

    private bool _isExpanded;

    public string Name { get; }

    public bool IsExpanded
    {
        get
        {
            return _isExpanded;
        }
        set
        {
            TreeViewItemExpanded();
            _isExpanded = value;
            OnPropertyChanged(nameof(IsExpanded));
        }
    }

    public TreeViewItemViewModel(string name)
    {
        Name = name;
    }

    public ObservableCollection<TreeViewItemViewModel> Items { get; } = new ObservableCollection<TreeViewItemViewModel>();

    protected abstract void TreeViewItemExpanded();

    protected void OnTreeViewItemExpanded(DirectoryInfo info)
    {
        Items.Clear();

        foreach (DirectoryInfo subDir in info.GetDirectories())
        {
            Items.Add(new DirectoryTreeViewItemViewModel(subDir));
        }
    }
}

public class DummyTreeViewItemViewModel : TreeViewItemViewModel
{
    public DummyTreeViewItemViewModel() : base(string.Empty)
    {
    }

    protected override void TreeViewItemExpanded()
    {
    }
}

public class DirectoryTreeViewItemViewModel : TreeViewItemViewModel
{
    public DirectoryTreeViewItemViewModel(DirectoryInfo directory) : base(directory.Name)
    {
        Directory = directory;
        Items.Add(new DummyTreeViewItemViewModel());
    }

    public DirectoryInfo Directory { get; }


    protected override void TreeViewItemExpanded()
    {
        OnTreeViewItemExpanded(Directory);
    }
}

public class DriveTreeViewItemViewModel : TreeViewItemViewModel
{
    public DriveTreeViewItemViewModel(DriveInfo drive) : base(drive.Name)
    {
        Drive = drive;
        Items.Add(new DirectoryTreeViewItemViewModel(Drive.RootDirectory));
    }

    public DriveInfo Drive { get; }

    protected override void TreeViewItemExpanded()
    {
        OnTreeViewItemExpanded(Drive.RootDirectory);
    }
}

MainViewModel是一个TreeView DateContext。希望你能使它发挥作用。