C# WPF TreeView绑定命令并传递单击的元素
如何将命令绑定到TreeView中的元素。我有一个带有TreeView命令的MainWindowViewModel,我试图在元素周围添加按钮,但该命令不被调用。是否有其他方法调用命令并传递单击的元素C# WPF TreeView绑定命令并传递单击的元素,c#,.net,wpf,treeview,C#,.net,Wpf,Treeview,如何将命令绑定到TreeView中的元素。我有一个带有TreeView命令的MainWindowViewModel,我试图在元素周围添加按钮,但该命令不被调用。是否有其他方法调用命令并传递单击的元素 <TreeView x:Name="MainTreeView" HorizontalAlignment="Stretch" Margin="10" VerticalAlignment="Stretch" ItemsSource="{Binding Departments}">
<TreeView x:Name="MainTreeView" HorizontalAlignment="Stretch" Margin="10" VerticalAlignment="Stretch" ItemsSource="{Binding Departments}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Positions}" DataType="{x:Type VM:Department}">
<Button Command="{Binding TreeViewCommand}" CommandParameter="{Binding DepartmentName}" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" >
<Label Content="{Binding DepartmentName}"/>
</Button>
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Employees}" DataType="{x:Type VM:Position}">
<Button BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" >
<Label Content="{Binding PositionName}"/>
</Button>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate DataType="{x:Type VM:Employee}">
<Button BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" >
<Label Content="{Binding EmployeeName}"/>
</Button>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
我的主窗口视图模式是
public class MainWindowViewModel : ViewModelBase
{
private List<Department> departments;
public MyICommand<string> TreeViewCommand { get; private set; }
public MainWindowViewModel()
{
TreeViewCommand = new MyICommand<string>(myTreeViewCommand);
Departments = new List<Department>()
{
new Department("DotNet"),
new Department("PHP")
};
}
public List<Department> Departments
{
get
{
return departments;
}
set
{
departments = value;
OnPropertyChanged("Departments");
}
}
public void myTreeViewCommand(string par)
{
Console.ReadKey();
}
}
公共类MainWindowViewModel:ViewModelBase
{
私人名单部门;
公共MyICommand树视图命令{get;private set;}
公共主窗口视图模型()
{
TreeViewCommand=新的MyICommand(myTreeViewCommand);
部门=新列表()
{
新部门(“DotNet”),
新部门(“PHP”)
};
}
公开名单部门
{
得到
{
返回部门;
}
设置
{
部门=价值;
房地产变更(“部门”);
}
}
公共无效myTreeViewCommand(字符串参数)
{
Console.ReadKey();
}
}
由于您的TreeView命令位于TreeView范围内,因此它将Departments对象作为其DataContext,因此无法找到该命令。您应该明确定义定义TreeViewCommand的DataContext。请执行以下操作:
<!-- Note the ElementName and Path=DataContext. -->
<Button Command="{Binding ElementName=MainTreeView, Path=DataContext.TreeViewCommand}" CommandParameter="{Binding DepartmentName}" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" >
<Label Content="{Binding DepartmentName}"/>
</Button>
所有节点的XAML:
<TreeView x:Name="MainTreeView" HorizontalAlignment="Stretch" Margin="10" VerticalAlignment="Stretch" ItemsSource="{Binding Departments}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Positions}">
<Button Command="{Binding ElementName=MainTreeView, Path=DataContext.TreeViewCommand}" CommandParameter="{Binding DepartmentName}" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" >
<Label Content="{Binding DepartmentName}"/>
</Button>
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Employees}">
<Button Command="{Binding ElementName=MainTreeView, Path=DataContext.TreeViewCommand}" CommandParameter="{Binding PositionName}" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" >
<Label Content="{Binding PositionName}"/>
</Button>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<Button Command="{Binding ElementName=MainTreeView, Path=DataContext.TreeViewCommand}" CommandParameter="{Binding EmployeeName}" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" >
<Label Content="{Binding EmployeeName}"/>
</Button>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
注意!如果最终希望传递对象而不是字符串(节点名称),则必须为每个按钮定义ICommand。谢谢!!这就是我需要的@菲利普菲利波维奇很乐意帮忙!:)我还为您添加了一个完整的XAML示例。看看这个。