gpt4 book ai didi

c# - 从 UserControl 访问类方法(XAML、C#、TreeView)

转载 作者:行者123 更新时间:2023-12-04 16:59:57 26 4
gpt4 key购买 nike

我尝试在 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

最佳答案

答案更新:

好的,这是完整的答案:

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>

View 模型:
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。希望你能让它工作。

关于c# - 从 UserControl 访问类方法(XAML、C#、TreeView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43997920/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com